Skip to content

Not Everything Needs a WebSocket: Choosing the Right Real-Time Transport

6 min read7 views★ featured[Backend]
Cover image for "Not Everything Needs a WebSocket: Choosing the Right Real-Time Transport"

Not Everything Needs a WebSocket: Choosing the Right Real-Time Transport

WebSockets have this reputation of being the answer to real-time on the web. And honestly, that reputation isn't entirely undeserved — they're genuinely great for what they were designed for. But over time I've seen them get reached for in situations where they're overkill, or worse, the wrong fit entirely.

Let me walk through what your actual options are, when each one makes sense, and where WebSockets specifically earn their keep.


What WebSockets Actually Are

A WebSocket is a persistent, bidirectional TCP connection that starts its life as an HTTP request and gets "upgraded." Once that upgrade happens, both the client and the server can send messages to each other at any time, in any order, without waiting for a request first.

That last part is the key: both directions, any time. That's what makes WebSockets feel special compared to plain HTTP.

But "both directions, any time" is also a cost. You're holding a connection open. You're managing state. You need infrastructure that supports long-lived connections (not every proxy or load balancer does, by default). And you're responsible for reconnection logic when things drop.

So before you default to WebSockets, ask yourself: do I actually need both directions? Do I need them at the same time?


Polling: The One Everyone Underestimates

Polling is exactly what it sounds like — your client asks the server "anything new?" on a timer. Every few seconds, an HTTP request goes out, and the server responds with whatever it has (or nothing).

Client: "Anything new?" → Server: "Nope."
Client: "Anything new?" → Server: "Nope."
Client: "Anything new?" → Server: "Here's 3 new items."

It sounds dumb. It often isn't.

For a dashboard that refreshes order counts every 10 seconds, polling is completely fine. The latency between updates doesn't matter — you just need eventual delivery. Polling gives you that with zero infrastructure changes: it's just HTTP, it works through every proxy and CDN, it's trivially cacheable, and it scales horizontally without any coordination.

Long polling is the smarter variant: the server holds the request open until it has something to say, then responds. This cuts down on wasted requests when updates are infrequent. You get lower latency than regular polling with minimal complexity added.

Use polling when: your update frequency is low, latency tolerance is a few seconds, and you don't want to touch your infrastructure.


SSE: The Underused One

Server-Sent Events (SSE) is HTTP, but the response never ends. The server opens a connection and streams newline-delimited text events down to the client whenever it has something to say. The browser has a native EventSource API that handles reconnection automatically.

data: {"type": "notification", "msg": "Order #441 shipped"}\n\n
data: {"type": "count", "value": 17}\n\n

SSE is unidirectional — only server → client. That's both its limitation and its strength. Because it's plain HTTP, it works through all the same proxies and load balancers your API already uses. You don't need a separate WebSocket server or any special configuration.

For the vast majority of "real-time" UIs I've built — live feeds, notifications, progress updates, streaming LLM responses — SSE was the right call. Simpler to implement, easier to debug (it's just HTTP in devtools), and you don't have to manage connection state on the server.

The one gotcha: browsers cap concurrent SSE connections per domain (usually 6 in HTTP/1.1), so if a user opens many tabs, they'll compete. HTTP/2 makes this mostly irrelevant since it multiplexes.

Use SSE when: you need server-push with no client-to-server stream. Notifications, live counts, progress bars, streaming AI responses.


WebSockets: When You Actually Need Them

WebSockets shine when both sides need to talk at any time, frequently, with minimal overhead per message. The classic examples hold up:

  • Chat applications — you're sending and receiving messages at unpredictable intervals
  • Multiplayer games — position updates, game state, inputs flowing in both directions simultaneously
  • Collaborative editing — think Google Docs-style where your keystrokes go up and other users' changes come down in real time
  • Financial tickers — not just receiving prices but also sending orders or subscriptions

The real win is low per-message overhead. After the handshake, WebSocket frames are tiny compared to HTTP headers. When you're sending dozens of messages per second, that adds up.

What WebSockets don't give you for free: reliability, ordering guarantees, reconnection logic, and backpressure. You have to build all of that yourself (or use a library like Socket.IO that handles it). And your reverse proxy needs to be configured to support them — nginx needs proxy_set_header Upgrade $http_upgrade and friends.

Use WebSockets when: you have high-frequency, bidirectional communication where latency and message overhead matter.


WebRTC: Peer-to-Peer Is a Different Category

WebRTC is in a different class entirely. It's not a client-server protocol — it's designed for direct peer-to-peer communication between browsers or devices, usually for audio, video, and arbitrary data channels.

The usual architecture still has a server involved (a signaling server, often over WebSocket, plus STUN/TURN servers to handle NAT traversal), but once the connection is established, data flows directly between peers without hitting your server at all.

This is why WebRTC is used for:

  • Video calls (Google Meet, Jitsi)
  • Real-time audio streaming
  • P2P file transfers
  • Low-latency screen sharing

The tradeoff: WebRTC is significantly more complex to set up. NAT traversal, codec negotiation, ICE candidates, DTLS handshakes — it's a lot. If you don't have a reason to avoid your server being in the middle, you probably don't need WebRTC.

Use WebRTC when: you need peer-to-peer communication, media streaming, or you can't afford the round-trip latency through a server.


A Quick Decision Guide

NeedUse
Data updated every few seconds, latency finePolling
Infrequent updates, want lower latencyLong polling
Server pushes to client, no upstream neededSSE
Bidirectional, high-frequency messagingWebSockets
Peer-to-peer, media, or no-server dataWebRTC

The Default Problem

The reason WebSockets get overused is that they can do what everything else does. They're bidirectional, so you can fake a request-response model over them. They work in real time, so you can do what SSE does. They're just... more general-purpose.

But general-purpose usually means more complexity for your specific case. If your server only ever pushes data to the client, using WebSockets means you have an open bidirectional pipe that you're only using in one direction — plus all the operational overhead that comes with it, for no gain.

Pick the simplest thing that solves your problem. In my experience, most "real-time" features are actually just server-push, which means SSE. A step down from that is long polling. WebSockets are for when you've genuinely outgrown those.

The pattern I follow: start with SSE, upgrade to WebSockets only if I need the client to send a continuous stream of data back. Consider WebRTC only when peers need to talk directly.

It keeps the system simpler, and simpler systems are easier to debug at 2am.

# Comments (0)

No comments yet. Be the first.

0/1000