Can I use the Redis adapter with PM2 cluster mode?
Yes. Set exec_mode: 'cluster' and instances: 'max' in the PM2 ecosystem file. With the Redis adapter, messages broadcast in one PM2 cluster worker are delivered to clients connected to all workers. Set REDIS_URL in the env config.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Scaling Socket.io with Redis Adapter Multi-Server WebSocket Support
When running multiple Node.js instances, each only knows about its own connected clients. Without the Redis adapter, a message broadcast on one instance won't reach clients on other instances. The Redis adapter uses Redis pub/sub to broadcast across all instances.
Install @socket.io/redis-adapter and redis. Create a Redis pub client and a sub client (duplicate). Connect both, then apply io.adapter(createAdapter(pubClient, subClient)). Now messages broadcast on one instance are delivered to clients on all instances.
Yes. Use ip_hash in Nginx upstream to ensure the same client always connects to the same Node.js instance. This is important for WebSocket connections because they're persistent. Without sticky sessions, the initial HTTP upgrade and subsequent WebSocket frames might go to different instances.
Still have questions?
Browse all our FAQs or reach out to our support team
