
7 WebSocket Scaling Patterns That Let Node.js Handle 1M Real-Time Connections
Most WebSocket demos die at 10,000 connections. Not because Node.js cannot handle more, but because the architecture is wrong. Here are 7 production patterns that take you from “it works locally” to “it survives 1 million concurrent connections”. 1. Single Instance Chat → Redis Pub/Sub Fan-Out A single ws server works until you add a second instance behind a load balancer. Before (single instance only): import { WebSocketServer } from ' ws ' ; const wss = new WebSocketServer ({ port : 3000 }); const clients = new Set < WebSocket > (); wss . on ( ' connection ' , ( ws ) => { clients . add ( ws ); ws . on ( ' message ' , ( msg ) => { for ( const client of clients ) { if ( client . readyState === ws . OPEN ) { client . send ( msg . toString ()); } } }); ws . on ( ' close ' , () => clients . delete ( ws )); }); Deploy this across 3 instances and messages disappear. Each process only knows about its own connections. After (Redis pub/sub bridge): import { WebSocketServer } from ' ws ' ; impo
Continue reading on Dev.to
Opens in a new tab




