This project demonstrating distributed systems concepts using WebSocket, Redis pub/sub, and job queues. This project shows how to build a backend architecture where multiple components communicate asynchronously through message queues and pub/sub patterns.
- WebSocket Server - Handles WebSocket connections, broadcasts messages, and queues jobs
- Redis Pub/Sub - Enables communication between different processes
- Job Queue (BullMQ) - Manages background tasks asynchronously
- Worker Process - Processes queued jobs and publishes results back via Redis
- Express - Web framework
- ws - WebSocket implementation
- BullMQ - Redis-based job queue
- ioredis - Redis client for Node.js
- redis - Redis client
- Docker - For running Redis locally
- Node.js (v16 or higher)
- Docker (for Redis)
- npm or yarn
- Clone the repository:
git clone https://github.com/Angshuman09/distributed-websocket-backend.git
cd distributed-websocket-backend- Start Redis with Docker:
docker run -d -p 6379:6379 redis:latest- Install dependencies for each component:
# Server dependencies (express, ws)
cd server
npm install
# Queue dependencies (bullmq)
cd ../queue
npm install
# Worker dependencies (bullmq, ioredis, redis)
cd ../workers
npm installStart each component in separate terminal windows:
# Terminal 1: WebSocket server
cd server
npm run dev
# Terminal 2: Queue processor
cd queue
npm run dev
# Terminal 3: Worker process
cd workers
npm run devThe screenshot shows the message flow:
- Client sends:
{"msg":"hi I am here :)","time":1772710371837} - Client sends:
{"msg":"good to see u are here ","time":1772710399261} - Server broadcasts:
hi I am here :)
The server component:
- Creates a WebSocket server on port 3000
- Handles client connections and disconnections
- Broadcasts incoming messages to all connected clients
- Adds jobs to the BullMQ queue for processing
The queue system:
- Stores jobs in Redis
- Manages job state (waiting, active, completed, failed)
- Delivers jobs to worker processes
- Provides job persistence and retry mechanisms
The worker:
- Listens for jobs from the queue
- Processes job data (message transformation, computation, etc.)
- Publishes results to Redis pub/sub channel
- Operates independently from the WebSocket server
Redis enables:
- Communication between worker and WebSocket server
- Decoupled architecture (worker doesn't need to know about clients)
- Message broadcasting across different processes
distributed-websocket-backend/
├── server/ # WebSocket server implementation
├── queue/ # Job queue management
├── workers/ # Background worker processes
├── public/ # Static assets and documentation
└── README.md # Project documentation
- Client sends message → WebSocket server receives it
- Server broadcasts → Message sent to all connected clients
- Server adds job → Job queued for processing
- Queue delivers → Job sent to available worker
- Worker processes → Executes job logic
- Worker publishes → Result sent to Redis pub/sub
- Server subscribes → Receives processed result from Redis
- Server broadcasts → Final result sent to clients
To understand this project better, learn about:
- WebSocket Protocol - Real-time bidirectional communication
- Redis - In-memory data structures and pub/sub messaging
- Job Queues - Background task processing patterns
- Distributed Systems - How independent services communicate
Angshuman09
- GitHub: @Angshuman09

