- Overview
- Project Structure
- Installation
- Running the Application
- API Documentation
- WebSocket Communication
- Testing
- Dependencies
- Contributing
- License
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.
- ├── 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 `
-
Clone the repository:
$ git clone https://github.com/MohamedAEmara/fastify-socket-server.git
$ cd fastify-socket-server
-
Install the dependencies:
npm install
-
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.
-
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
-
The server will be running at http://localhost:3000.
You can see Swagger documentation and try it out through the deployed server
OR
You can run the server locally and see Swagger documentation at /documentaion
endpoint
- 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
The WebSocket server is implemented using the ws library. It enables bidirectional communication between the client and server.
- Open Postman.
- Create a new WebSocket request to ws://localhost:3000.
- Send a message to the server.
- 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.
To test the WebSocket server using Postman:
- Import the Postman collection from test/socketTest.json.
- Establish a WebSocket connection to ws://localhost:3000.
- Send test messages and verify the responses.
33e4-d94c-47f3-963e-b66a52d54de1.mp4
- Fastify - Fast and low overhead web framework for Node.js
- ws - Simple to use, blazing fast and thoroughly tested WebSocket client and server for Node.js
- fastify-swagger - Fastify plugin to serve Swagger UI and JSON
- bcrypt - For hashing user passwords
- @prisma/client - ORM For easier dealing with postgres
Contributions are welcome! Please submit a pull request or open an issue to discuss your ideas.
For any inquiries or support, please contact Mohamed Emara.