Engineering
Real-time features architecture: WebSockets, SSE, and scaling past the demo
Pick the right transport for chat, dashboards, and presence. Practical architecture patterns for real-time features that hold up under real load.
TL;DR
SSE covers most server-to-client use cases with far less complexity than WebSockets. Use WebSockets when clients send frequently too, add a pub/sub backbone when you run more than one server, and design for reconnection from day one.

The right transport for a real-time feature depends on one question: does data flow one way or both ways? Server-Sent Events handle most server-to-client use cases with less complexity than teams expect. WebSockets are the correct call when clients send frequently too, but reaching for them by default adds operational weight you may not need.
Match the transport to the direction of data flow
SSE is a one-way stream from server to client over plain HTTP. It auto-reconnects, works through most proxies and load balancers without special configuration, and requires no protocol upgrade. Notifications, live feeds, activity streams, and read-only dashboards all fit here. It is the right answer more often than its reputation suggests.
WebSockets open a persistent, full-duplex connection. Use them for chat, multiplayer, collaborative editing, and live cursors: anything where the client is sending a constant stream of events back to the server. The bidirectionality is the point.
Polling (and its cousin long-polling) asks the server repeatedly on a timer. It is crude and wastes resources, but occasionally it is the pragmatic choice for infrequent updates or environments where persistent connections are unreliable. Do not reach for it by default.
The practical rule: if clients only receive, SSE. If clients also send constantly, WebSockets.
Managed real-time layers exist for a reason
Persistent connections are easy to prototype and genuinely hard to operate. Before writing your own WebSocket server, it is worth understanding what you are signing up for: connection lifecycle management, heartbeats, graceful shutdown, backpressure, and the scaling problem described below.
Managed layers solve most of this:
- Supabase Realtime broadcasts Postgres row changes and presence state over WebSockets with almost no server code, a natural fit if you are already on Supabase
- Pusher, Ably, and similar handle connection scaling, delivery guarantees, and fan-out for you at a per-message or per-connection price
- Framework-level options (Action Cable, Laravel Echo, etc.) give you batteries-included real-time tied to your existing stack
Building your own is justified by specific requirements or scale economics, not by a preference for control.
The architecture pattern that scales
One server holding a handful of WebSocket connections is simple. Ten servers holding ten thousand connections each is a different problem. A client connected to server A must receive messages published on server B. Without a shared layer, they cannot.
The standard solution is a pub/sub backbone, typically Redis, sitting behind your connection servers:
- Each connection server holds its own open client connections
- When an event occurs, the application publishes it to a channel on Redis
- Every connection server subscribes and pushes the message to whichever clients it holds
This separates "who is connected where" from "what needs to be delivered," which lets you add connection servers horizontally without any of them needing to know about each other. Managed real-time services implement this pattern internally so you never see it, which is much of what you are paying for.
For teams building on Next.js and NestJS, this pattern maps cleanly: NestJS handles the WebSocket gateway and publishes to Redis; each gateway instance subscribes and fans out to its own clients. The microservices vs monolith choice affects where that gateway lives, but the pub/sub pattern itself is the same either way.
The failure cases that kill real-time features in production
Most "real-time is hard" problems come from skipping the reliability layer, not from picking the wrong transport.
Reconnection. Mobile networks drop. Laptops sleep. Proxies close idle connections. Clients must reconnect automatically with exponential backoff. If your client library does not do this, write it yourself. This is not optional.
Missed messages. After a reconnect, the client has a gap. The two ways to fill it: replay events since a cursor or timestamp the client sends on reconnect, or refetch full state on reconnect and treat it as a fresh start. The right choice depends on how expensive a full refetch is. For most dashboards, refetch is simpler. For chat, replay is expected.
Presence. "Who is online" requires heartbeats from the client and a timeout on the server to detect silent disconnects. A user whose connection dies without a clean close will otherwise appear online indefinitely. Supabase Realtime and Ably handle this; if you build it yourself, account for it explicitly.
Ordering and conflicts. For collaborative editing, you need a strategy for simultaneous writes. Last-write-wins is simple and often acceptable. CRDTs or operational transformation handle true concurrent editing without data loss, but they add significant complexity. Most products do not need them.
Authentication. Authenticate at the handshake and authorize each channel subscription server-side. Do not trust channel names or room IDs sent by the client without validating them against the user's permissions. This is the same principle as secure API design applied to connection state.
The practical path
Use the simplest transport that meets the actual need, SSE far more often than teams default to. Lean on a managed real-time layer unless economics or requirements push you off it. Design for reconnection and message gaps before you ship, not after the first complaint. Those three decisions resolve most of the complexity before you write a line of connection handling code.
If you are adding real-time features and want an architecture that holds up past the demo, let's talk.
Frequently asked questions
When should I use SSE instead of WebSockets?
Any time data flows only from server to client: notifications, live feeds, activity streams, dashboards. SSE auto-reconnects, works through most proxies, and requires no special protocol upgrade. It is underused because developers default to WebSockets without thinking through the direction of data flow.
How do you scale WebSocket connections across multiple servers?
Put a pub/sub backbone (Redis is the common choice) behind your connection servers. Each server holds its own open connections, and when an event is published it goes to Redis, which fans it out to every connection server that has a subscriber. This decouples connection state from message delivery.
How do you handle missed messages after a WebSocket reconnect?
Track a cursor or timestamp on the client. On reconnect, send that cursor to the server and replay any events since that point. For state-heavy features, it is sometimes simpler to refetch the full current state on reconnect rather than replay a potentially long event log.
Should I build my own WebSocket server or use a managed service?
Use a managed layer (Supabase Realtime, Pusher, Ably) unless you have specific requirements those services cannot meet or the per-connection cost becomes material at your scale. The operational complexity of running persistent connection infrastructure at scale is routinely underestimated.
How Pykero can help
Services related to this article.
Building something like this?
Pykero Agency designs and ships production web, mobile, SaaS, and AI products.
Talk to us


