https://github.com/mohamedaemara/fastify-socket-server
https://github.com/mohamedaemara/fastify-socket-server
fastify nodejs swagger
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/mohamedaemara/fastify-socket-server
- Owner: MohamedAEmara
- Created: 2024-05-14T07:29:24.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2024-05-16T19:35:13.000Z (about 2 years ago)
- Last Synced: 2025-05-19T11:45:56.387Z (about 1 year ago)
- Topics: fastify, nodejs, swagger
- Language: JavaScript
- Homepage: https://fastify-socket-server.onrender.com/documentation/
- Size: 112 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Fastify Socket Server
## Table of Contents
- [Overview](#overview)
- [Project Structure](#project-structure)
- [Installation](#installation)
- [Running the Application](#running-the-application)
- [API Documentation](#api-documentation)
- [WebSocket Communication](#websocket-communication)
- [Testing](#testing)
- [Dependencies](#dependencies)
- [Contributing](#contributing)
- [License](#license)
## Overview
This project is a Fastify application that implements CRUD operations for a User entity and integrates a WebSocket server to enable bidirectional communication between the client and server. The application includes Swagger documentation for the Fastify endpoints and a Postman collection for testing the WebSocket server.
## Project Structure
- ├── src
- │ ├── controllers
- │ │ └── userController.js
- │ ├── db
- │ │ └── db.js
- │ ├── routes
- │ │ └── user.route.js
- │ ├── controllers
- │ │ └── user.controller.js
- │ ├── services
- │ │ └── user.service.js
- │ └── app.js
- │── socket-server
- │ └─── socketServer.js
- │── prisma
- │ ├── migrations
- │ └─── schema.prisma
- ├── .gitignore
- ├── package.json
- ├── package-lock.json
- ├── docker-compose.yml
- └── README.md
`
## Installation
1. Clone the repository:
$ git clone https://github.com/MohamedAEmara/fastify-socket-server.git
$ cd fastify-socket-server
2. Install the dependencies:
npm install
3. Add .env file and add your enviroment variables
```
HOST=localhost
PORT=3000
PASSWORD_SALT="$fdb$10$wH8qdfswE5R3C.6v5fdsZ/ZmM"
JWT_SECRET="secretvalue123"
DATABASE_URL="postgresql://postgres:1234@localhost:5432/fastifysocket"
## NOTE: this data base will connect to DB docker container defined in docker-compse.
```
#### Make sure you installed Postgres on your machine
### OR
#### Use the DB configurations in docker-compose.yml file (Make sure you installed Docker)
## Running the Application
1. Start the Fastify server: **this will run DB container and connect fastify server to it. Also will run the socker server**
``
npm run start:dev
``
In case you don't want to use Docker and have Postgres installed on your machine RUN
``
npm run start
``
2. The server will be running at http://localhost:3000.
## API Documentation
You can see Swagger documentation and try it out through the ``deployed server``
- https://fastify-socket-server.onrender.com/documentation/
OR
You can run the server locally and see Swagger documentation at ``/documentaion`` endpoint
- http://localhost:3000/documentation
### User Endpoints
- **GET /users:** Retrieve all users
- **GET /users/:id:** Retrieve a user by ID
- **POST /users:** Create a new user
- **PUT /users/:id:** Update a user by ID
- **DELETE /users/:id:** Delete a user by ID
## WebSocket Communication
The WebSocket server is implemented using the ws library. It enables bidirectional communication between the client and server.
### How to Test WebSocket
1. Open Postman.
2. Create a new WebSocket request to ws://localhost:3000.
3. Send a message to the server.
4. The server will log the received message and send a response back to the client.
For more detailed testing, you can use the provided Postman collection.
## Testing
To test the WebSocket server using Postman:
1. Import the Postman collection from test/socketTest.json.
2. Establish a WebSocket connection to ws://localhost:3000.
3. Send test messages and verify the responses.
## Postman Testing
https://github.com/MohamedAEmara/Fastify-Socket-Server/assets/62681515/7d78ccf9-39d9-4cb5-a23e-b2c8d611f923
## Dependencies
- [Fastify](https://www.npmjs.com/package/fastify) - Fast and low overhead web framework for Node.js
- [ws](https://www.npmjs.com/package/ws) - Simple to use, blazing fast and thoroughly tested WebSocket client and server for Node.js
- [fastify-swagger](https://www.npmjs.com/package/fastify-swagger) - Fastify plugin to serve Swagger UI and JSON
- [bcrypt](https://www.npmjs.com/package/bcrypt) - For hashing user passwords
- [@prisma/client](https://www.npmjs.com/package/@prisma/client) - ORM For easier dealing with postgres
## Contributing
Contributions are welcome! Please submit a pull request or open an issue to discuss your ideas.
## Contact
For any inquiries or support, please contact [Mohamed Emara](mailto:mohamedemara.dev@gmail.com).