What is Redis Pub/Sub and how does it help with WebSocket scaling?

Answer

When WebSocket servers scale horizontally, a message sent to a client on Server A cannot reach a client connected to Server B — they have separate in-memory connection pools. Redis Pub/Sub solves this by acting as a message broker between server instances. Each server instance subscribes to Redis channels relevant to its clients. When Server A needs to send a message to all users in room "game-42", it publishes to the Redis channel room:game-42. Redis delivers that message to all subscribers — Server A, B, and C all receive it and each forward it to their local connections in that room. Socket.IO's @socket.io/redis-adapter implements this pattern automatically: you just configure the Redis connection and io.to(room).emit() works correctly across all instances without code changes.