The Single-Server WebSocket Trap

In-memory WebSockets work smoothly in development. But the moment you scale your Node.js backend across 4 Docker containers behind an Nginx load balancer, a client connected to Server A cannot receive messages broadcast from Server B.

Redis Streams as a Distributed Pub/Sub Backbone

Redis Streams provide persistent, consumer-group distributed message queuing that guarantees delivery even across worker container restarts.

// Broadcasting events across distributed WebSocket nodes
async function broadcastEvent(channel, payload) {
  await redis.xadd(`stream:${channel}`, '*', 'data', JSON.stringify(payload));
}