WebSockets in production: chat, notifications and live dashboards that stay connected
Realtime always works on localhost. Behind a load balancer, across several instances and on a phone on a train, it's another story.
Co-founder & Head of Development and Communications, Parslinks
A team builds a live-auction app for vintage cameras. In development, bids appear instantly on every screen. In production, users complain that the current price freezes for a minute, then jumps, and twice someone has been outbid without seeing it.
Three culprits turn up. The load balancer closes idle connections after 60 seconds. Phones drop off the network every time a train enters a tunnel. And because the app runs two instances, a bid placed on instance A never reaches a watcher connected to instance B.
First question: do you really need WebSockets?
A WebSocket is a persistent, two-way channel. Powerful, but each open connection costs memory and adds a new way to fail. Often a simpler option is enough.
Polling (a request every few seconds) is perfectly fine for an order status that changes every few minutes, and it works on any infrastructure.
Server-Sent Events (SSE) give you a one-way stream from server to browser over plain HTTP. Great for notifications, progress bars and live dashboards, and the browser reconnects automatically.
WebSockets are for when both sides talk continuously: chat, collaborative editing, games, or an auction where every bid matters.
Three things to get right in production
1. Heartbeats
Proxies and load balancers close idle connections. Send a small ping every 20 to 30 seconds so the connection stays alive and both ends notice quickly when the other is gone.
2. Reconnect with backoff
On mobile, disconnects are normal, not exceptional. The client should reconnect by itself: after one second, then two, four, up to 30, with some jitter so thousands of clients don't stampede the server at the same moment. After reconnecting, fetch the latest state instead of assuming nothing was missed.
3. Many instances, one message bus
With more than one instance, each knows only its own clients. The usual fix is a shared pub/sub channel such as Redis: every instance publishes events, and every instance forwards them to its own clients. Libraries like Socket.IO ship adapters for this.
Sticky sessions help, but they don't replace a shared bus. A bidder and a watcher are different users and can land on different instances.
The outcome
The auction team added a 20-second heartbeat, rewrote the client to reconnect and resume from the last bid ID, and fanned events out through Redis. The "my watchlist" page, which only changes every few minutes, moved from WebSockets to SSE and freed half the server's open connections.
Frequently asked questions
Join the discussion
No comments yet. Be the first to share your thoughts.
Related posts
- Platform
Serverless, containers or PaaS? Three runtime models with one real application
Serverless is exciting at conferences, containers in job ads, and PaaS in projects that have to ship on time. This post takes one real shop API through all three models and shows where each one shines and where it causes trouble.
- Deploy
Node.js hosting: shared hosting, a VPS or a platform?
Many people put their first Node.js app on the same cPanel host as their WordPress site, then find out why it dies every few hours. This post explains what Node.js needs from a host and compares the three common options.
- Deploy
Zero-downtime deployments in practice: rolling, blue/green, and promoting the same artifact
A restart is the simplest way to deploy, and the seconds the service is down are the seconds orders fail. This post lists where downtime really comes from, compares three common release strategies, and shows how to change your database without an outage.
