This project implements a real-time chatroom microservice using Socket.io for WebSocket communication and In-Memory Storage for storing chat history and active users. It supports:
- Joining and leaving chatrooms
- Sending and receiving messages in real-time
- Retrieving active users and chat history
- Node.js (v14+ recommended)
- Install dependencies:
npm install
- Start the server:
npm run start
The server will run on http://localhost:3000.
-
Open Postman.
-
Select Socket.IO Request from the request type dropdown.
-
Enter the URL:
http://localhost:3000 -
Click Connect.
-
Navigate to the Events tab in Postman and manually add event listeners for join, message, leave and error events to receive real-time updates.
Once connected, send the following event:
- Event Type:
join - Payload:
{ "userId": "1", "chatRoomId": "1" }
Send a message event with the following payload:
{
"userId": "1",
"chatRoomId": "1",
"message": "Hello, world!",
"timestamp": "2024-01-29T12:00:00Z"
}Send a leave event with the following payload:
{
"userId": "1",
"chatRoomId": "1"
}Returns a list of currently active users in a chatroom.
GET /chat/active-users/:chatRoomIdReturns the last N messages from a specific chatroom (default: 50 messages).
GET /chat/chat-history/:chatRoomId?limit=50Jest is used for testing the service. Run the tests using:
npm run test