A demonstration project showcasing a scalable Socket.IO implementation using NestJS. This project implements a horizontally scalable WebSocket server architecture that can handle real-time communications across multiple server instances.
The project consists of the following components:
- Socket.IO Server: NestJS-based WebSocket server with adapter for horizontal scaling
- Consumer Service: NestJS microservice that processes messages and broadcasts them to Socket.IO clients
- Adapters: Used for Socket.IO pub/sub between multiple server instances. eg. Redis
- Message Brokers: for handling event distribution. eg. RabbitMQ
- Horizontally scalable Socket.IO server using adapters
- Message queuing for reliable event processing
- Microservice architecture using NestJS
- Docker containerization for easy deployment
- Receives events from client and emit to message broker
- Docker and Docker Compose
- Node.js (for local development)
- PNPM package manager
- Clone the repository:
git clone git@github.com:civilcoder55/scalable-nest-websockets.git
cd scalable-nest-websockets
- Start the services using Docker Compose:
docker-compose up -d --scale socket-io-server=3
This will start:
- RabbitMQ (Management UI available at http://localhost:15672)
- Redis
- Socket.IO Server (3 instances available at http://localhost:3000, http://localhost:3001, http://localhost:3002)
- Consumer Service
- Connect to the Socket.IO server using a socket.io client (e.g., Postman)
- Send a message to RabbitMQ's 'notification' queue
- The consumer will process the message and broadcast it via Socket.IO
- All connected clients will receive the notification
REDIS_URL
: Redis connection URL
RABBITMQ_URL
: RabbitMQ connection URLREDIS_URL
: Redis connection URL
For local development:
- Install dependencies:
pnpm install
- Start services in development mode:
# Terminal 1 - Start dependencies
docker-compose up redis rmq
# Terminal 2 - Start Socket.IO server
cd socket-io-server
pnpm run start:dev
# Terminal 3 - Start consumer
cd consumer
pnpm run start:dev
Feel free to submit issues, fork the repository, and create pull requests for any improvements.