This project is a scalable chat application built with Redis Pub/Sub and Socket.IO. It addresses the challenge of scaling real-time communication by enabling multiple servers to share messages seamlessly using Redis as a message broker.
In a typical chat application:
-
Single Server Scenario:
- Users connect to a single server.
- Messages are exchanged effortlessly.
-
Multi-Server Scenario:
- When the user base grows, new servers are added to distribute the load.
- Users connected to different servers cannot communicate, breaking the real-time chat experience.
How can users on different servers exchange messages seamlessly?
To solve this, we introduce Redis Pub/Sub as a message broker:
- Redis Pub/Sub allows servers to publish and subscribe to messages on specific channels.
- Each chat server subscribes to a common Redis channel.
- When a user sends a message:
- The server publishes the message to the Redis channel.
- Redis broadcasts the message to all subscribed servers.
- Each server relays the message to its connected users.
-
Socket.IO:
- Enables real-time, bi-directional communication between the server and clients.
-
Redis Pub/Sub:
- Acts as a message broker to sync messages across multiple servers.
-
Server Instances:
- Multiple server instances can handle different users but remain in sync through Redis.
-
User sends a message:
- The connected server publishes the message to a Redis channel.
-
Redis distributes the message:
- All subscribed servers receive the message.
-
Server broadcasts to clients:
- Each server broadcasts the received message to its connected users via Socket.IO.
- User 1 connects to Server 1 and sends a message: "Hello!"
- User 4 connects to Server 2.
- Server 1 publishes the message "Hello!" to the Redis channel.
- Server 2 receives the message from Redis and broadcasts it to User 4.
- Both User 1 and User 4 see the message "Hello!" in real-time.
-
Scalability:
- Add more servers as user traffic grows.
-
Seamless Communication:
- Users on different servers can communicate without issue.
-
Fault Tolerance:
- If one server fails, others continue operating independently.

