logo
Parslinks
Back to blog
Guides4 min read

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.

Alireza Tadi

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, SSE or WebSockets?

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.

realtime-client.ts
function connect(attempt = 0) {
  const ws = new WebSocket(url);
  ws.onopen = () => { attempt = 0; ws.send(JSON.stringify({ type: 'resume', since: lastEventId })); };
  ws.onclose = () => {
    const base = Math.min(30_000, 1000 * 2 ** attempt);
    setTimeout(() => connect(attempt + 1), base / 2 + Math.random() * base / 2);
  };
}
ts8 lines

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.