This WebSocket server is a real-time communication backend designed to handle student submission results. It integrates with Redis to manage connections and uses Pub/Sub for processing updates from the Result Worker. This architecture supports scalability by allowing multiple WebSocket servers to work in parallel, all subscribing to the same Pub/Sub channels.
- Pub/Sub Integration: Listens to
positive-processedandnegative-processedchannels for real-time updates. - WebSocket Connection Management: Stores and retrieves client connections using Redis, mapped to their respective student IDs.
- Scalable Architecture: Multiple WebSocket servers can subscribe to the same Pub/Sub channels for horizontal scaling.
- Real-Time Notifications:
- Sends a
positivemessage to students with verified submissions. - Sends a
negativemessage to students with failed verification, triggering their system to shut down.
- Sends a
-
Connection Management:
- When a client connects, their WebSocket
remoteAddressis stored in Redis, mapped to their student ID. - If a client disconnects, the mapping is removed.
- When a client connects, their WebSocket
-
Message Handling:
- On receiving a message from the
positive-processedchannel, the server sends apositivesignal to the relevant client. - On receiving a message from the
negative-processedchannel, the server sends anegativesignal to the relevant client, shutting down the student's system and notifying authorities.
- On receiving a message from the
-
Scalability:
- Multiple WebSocket servers can be deployed, all subscribing to the same Pub/Sub channels for distributed processing.
- Node.js (version >= 16)
- Redis (for Pub/Sub and connection management)
- Docker (optional, for running Redis in a container)
- Git
Follow these steps to set up the WebSocket server in your environment:
- Clone the repository:
git clone https://github.com/priyanshubisht10/websocket-server.git cd websocket-server - Install dependencies:
npm install
- Make sure your redis-stack is up and running and the worker is able to publish to the pub/sub. If you haven't installed the worker it yet, you can find the steps here - (https://github.com/priyanshubisht10/result-worker)
- Build
tsc -b
- Start the server:
node dist/app.js
- Verify the setup: If everything is set up correctly, you should see the following log in the terminal:
Server is listening on port 3000
For a detailed explanation of the architecture, functionality, and use cases of this WebSocket server, check out this Medium article by me:
https://priyanshubisht10.medium.com/pub-sub-and-websockets-d8d180c2b9e8