This is a simple WebSocket chat application built with Fastify and ws library.
- Allows users to connect via WebSocket and send messages to each other.
- Users are authenticated using JWT tokens.
- Real-time bidirectional communication between clients and server.
- Fastify (Fast and low overhead web)framework, for Node.js
- Typescript
- WebSocket (ws library)
- JWT (JSON Web Tokens)
- MongoDB (for user modeling)
- Clone the repository:
git clone https://github.com/yousefshabaneg/fastify-users-websockets.git
- Install dependencies:
cd fastify-users-websockets
npm install
- Set up environment variables:
Create a .env
file in the root directory and add the following:
API_PORT =
WS_PORT =
NODE_ENV =
#Database
DATABASE_URI = ""
#jwt
JWT_SECRET =
JWT_EXPIRES_IN =
Rename the .env.example to .env and fill the variables with your own values.
- Start the server:
npm start
- The server will start running on your API_PORT or by default at 3000.
- The Websocket server will start running on your WS_PORT or by default at 3030.
- Connect to the WebSocket server using a WebSocket client or library.
- Send messages to other connected users by specifying the recipient's user ID and the message content like:
ws://localhost:3030?token=your-token
{
"content": "Random Message",
"to": "put here the user object id"
}
- Receive messages from other users in real-time.
-
Postman API documentation can be accessed at: Documentation Postman
-
Swagger API documentation can be accessed at
/documentation
route after starting the server
Contributions are welcome! Feel free to open issues or submit pull requests for any improvements or bug fixes.