0
0
RedisHow-ToBeginner · 4 min read

How to Use Redis for WebSocket Scaling: Simple Guide

To scale WebSocket connections across multiple servers, use Redis Pub/Sub to broadcast messages between servers. Each WebSocket server subscribes to Redis channels and publishes messages to them, ensuring all clients receive updates regardless of which server they are connected to.
📐

Syntax

Redis Pub/Sub uses two main commands: PUBLISH channel message to send messages, and SUBSCRIBE channel to listen for messages on a channel. WebSocket servers use these commands to share messages across instances.

  • PUBLISH: Sends a message to a channel.
  • SUBSCRIBE: Listens for messages on a channel.
redis
SUBSCRIBE channel_name
PUBLISH channel_name "message"
💻

Example

This example shows two WebSocket servers using Redis Pub/Sub to share messages. When one server receives a message from a client, it publishes it to Redis. The other server, subscribed to the same channel, receives the message and sends it to its connected clients.

javascript
const WebSocket = require('ws');
const Redis = require('redis');

// Create Redis clients for publishing and subscribing
const redisSub = Redis.createClient();
const redisPub = Redis.createClient();

// Subscribe to a Redis channel
redisSub.subscribe('chat_channel');

// Create WebSocket server
const wss = new WebSocket.Server({ port: 8080 });

wss.on('connection', (ws) => {
  // When a message is received from a client, publish it to Redis
  ws.on('message', (message) => {
    redisPub.publish('chat_channel', message);
  });

  // When a message is received from Redis, send it to the client
  redisSub.on('message', (channel, message) => {
    if (channel === 'chat_channel') {
      ws.send(message);
    }
  });
});

console.log('WebSocket server running on ws://localhost:8080');
Output
WebSocket server running on ws://localhost:8080
⚠️

Common Pitfalls

  • Not separating Redis clients: Using the same Redis client for publishing and subscribing can cause issues; use separate clients.
  • Missing message forwarding: Forgetting to forward Redis messages to WebSocket clients results in no cross-server communication.
  • Channel name mismatch: Ensure all servers subscribe and publish to the exact same channel name.
  • Scaling beyond Pub/Sub: Redis Pub/Sub does not persist messages; for guaranteed delivery, consider Redis Streams or other message brokers.
javascript
/* Wrong: Using one Redis client for both publish and subscribe */
const redis = Redis.createClient();
redis.subscribe('chat_channel');
redis.on('message', (channel, message) => { /* ... */ });
redis.publish('chat_channel', 'hello');

/* Right: Use separate clients */
const redisSub = Redis.createClient();
const redisPub = Redis.createClient();
redisSub.subscribe('chat_channel');
redisPub.publish('chat_channel', 'hello');
📊

Quick Reference

Use Redis Pub/Sub to share WebSocket messages across servers:

  • SUBSCRIBE channel: Listen for messages.
  • PUBLISH channel message: Send messages.
  • Use separate Redis clients for publishing and subscribing.
  • Forward Redis messages to all connected WebSocket clients.
  • Ensure all servers use the same channel name.

Key Takeaways

Use Redis Pub/Sub to broadcast WebSocket messages across multiple servers for scaling.
Always use separate Redis clients for publishing and subscribing to avoid conflicts.
Forward messages received from Redis to all connected WebSocket clients on each server.
Ensure all servers subscribe and publish to the same Redis channel name.
Redis Pub/Sub is fast but does not guarantee message delivery; consider other tools for persistence.