Script Valley
WebSockets & Real-Time Applications
Scaling WebSocket ServersLesson 6.3

Socket.IO Redis adapter for horizontal scaling

@socket.io/redis-adapter installation, createAdapter configuration, io.adapter(), pub/sub wiring, room broadcast across nodes, adapter internals, cluster mode

Drop-In Redis Adapter for Socket.IO

Socket.IO's Redis adapter replaces the in-memory broadcast mechanism with Redis pub/sub automatically. No changes to your event handlers are needed:

npm install @socket.io/redis-adapter ioredisconst { createClient } = require('redis'); const { createAdapter } = require('@socket.io/redis-adapter'); const { Server } = require('socket.io'); const http = require('http'); const server = http.createServer(); const io = new Server(server); const pubClient = createClient({ url: 'redis://localhost:6379' }); const subClient = pubClient.duplicate(); await Promise.all([pubClient.connect(), subClient.connect()]); io.adapter(createAdapter(pubClient, subClient)); io.on('connection', (socket) => { socket.join('general'); // io.to('general').emit() now works across all nodes io.to('general').emit('user:joined', { id: socket.id }); }); server.listen(3000);

Two separate Redis clients are required: one for publishing and one for subscribing, because a Redis connection in subscribe mode cannot issue other commands. The adapter handles all routing internally. io.to(room).emit() just works across nodes.

Up next

Sticky sessions and load balancing for WebSocket servers

Sign in to track progress

Socket.IO Redis adapter for horizontal scaling โ€” Scaling WebSocket Servers โ€” WebSockets & Real-Time Applications โ€” Script Valley โ€” Script Valley