Scaling WebSocket Connections with Redis Pub/Sub for Multi-Instance Node.js Applications
HexShift

HexShift @hexshift

About: Help with web development through niche, technical tutorials. From advanced JavaScript patterns and TypeScript best practices to mastering frameworks. Support link at base of each article. Thanks.

Joined:
Apr 5, 2025

Scaling WebSocket Connections with Redis Pub/Sub for Multi-Instance Node.js Applications

Publish Date: Apr 15
3 1

Scaling WebSocket Connections with Redis Pub/Sub for Multi-Instance Node.js Applications

WebSocket servers usually work great in single-instance deployments. But when you scale your application across multiple servers or containers, you run into a problem: clients connected to one instance can't communicate with clients connected to another. This is where Redis Pub/Sub comes to the rescue.

Why Redis Pub/Sub?

Redis Pub/Sub allows different processes (in our case, different WebSocket server instances) to subscribe to the same message channel. When a message is published, all subscribers receive it — enabling real-time messaging across your entire infrastructure.

Step 1: Install Dependencies

npm install ws redis

Step 2: WebSocket Server with Redis Integration

// server.js
const WebSocket = require('ws');
const { createClient } = require('redis');

const wss = new WebSocket.Server({ port: 8080 });
const redisPub = createClient();
const redisSub = createClient();

redisPub.connect();
redisSub.connect();

const CHANNEL = 'global_messages';
let clients = new Set();

wss.on('connection', (ws) => {
  clients.add(ws);

  ws.on('message', async (message) => {
    // Publish to Redis
    await redisPub.publish(CHANNEL, message.toString());
  });

  ws.on('close', () => {
    clients.delete(ws);
  });
});

// Listen for Redis messages and broadcast to local clients
redisSub.subscribe(CHANNEL, (message) => {
  clients.forEach((client) => {
    if (client.readyState === WebSocket.OPEN) {
      client.send(message);
    }
  });
});

Step 3: Test with Multiple Server Instances

You can now spin up multiple Node.js server instances, possibly on different ports or machines, and they'll all stay in sync via Redis. Just make sure each instance subscribes to the same Redis channel.

Step 4: A Simple Client

<!DOCTYPE html>
<html>
<body>
  <textarea id="chat" rows="10" cols="50"></textarea><br/>
  <input id="input" />
  <button onclick="sendMessage()">Send</button>

  <script>
    const socket = new WebSocket('ws://localhost:8080');
    const chat = document.getElementById('chat');

    socket.onmessage = (event) => {
      chat.value += event.data + "\n";
    };

    function sendMessage() {
      const input = document.getElementById('input');
      socket.send(input.value);
      input.value = '';
    }
  </script>
</body>
</html>

Conclusion

By integrating Redis Pub/Sub, you’ve unlocked horizontal scalability for your WebSocket infrastructure. This approach is production-ready, fast, and used by many real-time apps at scale. You can expand it further with namespaces, authentication layers, and message types for complex workflows.

If this post helped you, consider supporting me: buymeacoffee.com/hexshift

Comments 1 total

  • Nevo David
    Nevo DavidApr 16, 2025

    Amazing how easy it is to connect lots of servers together with Redis like this!

Add comment