Skip to main content
networking

WebSockets and SSE

12 min read
Fully authored

Two ways to keep the server talking to the client.

The old web is request/response: browser asks, server answers, connection closes. Modern apps need the opposite — the server pushing to the client whenever there is news. There are three ways to do this, and the choice locks in your delivery semantics, your infra cost, and your resilience story forever.

How WebSocket works

Client sends an HTTP request with Upgrade: websocket. Server responds 101 Switching Protocols. The TCP connection stays open and now speaks the WebSocket frame protocol. Either side can send bytes any time.

C → S: GET /ws HTTP/1.1
Upgrade: websocket
Sec-WebSocket-Key: dGhlIHNhbXBsZQ==
S → C: HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Sec-WebSocket-Accept: s3pPLMBiTxaQ9k...
C ↔ S: [frame] hello
S ↔ C: [frame] { "type": "message", ... }

Trade-off matrix

DimensionWebSocketSSELong-poll
DirectionBidirectionalServer → ClientClient-initiated
Wire cost per event~4 bytes overhead~10 bytes overhead~500 bytes (HTTP headers)
Auto-reconnectManual (write code)Built-in (browser)Automatic (next request)
Corporate proxy friendlySometimes blockedAlways worksAlways works
Backend infraSticky connections + WS serversAny HTTP serverAny HTTP server
Concurrent conn limit~65K per port (kernel)Browser caps at 6/domainSame as regular HTTP
Load balancer complexityL7 with session affinityStandard L7Standard L7
Latency for push<5 ms<10 ms0-30s (timeout window)

When to pick each

WebSocket

Bidirectional chat (Slack, Discord), collaborative editing (Figma), trading apps, multiplayer games. Any time the client needs to send data back on the same channel with the same latency guarantees.

SSE

LLM streaming (OpenAI, Anthropic), live sports scores, stock ticker, notification stream. When it's one-way and you want the SIMPLEST possible infrastructure. Falls back to HTTP naturally on proxies.

Long-poll

Fallback layer for browsers or corporate networks blocking WS. Simple mobile clients. Legacy integrations. NOT a primary choice for new systems — the header overhead alone makes it 100× more expensive than WS at scale.

The senior-engineer question:

You are designing a chat app. Interviewer asks: WS or SSE? Most candidates reflexively answer WS. The senior answer is: “WS for the message channel — clients need to send. SSE fallback for the notification-only channel on flaky mobile networks — 3× lower server cost per idle connection.” Show that you understand the per-channel decision, not the per-app decision.

References

  • RFC 6455 — The WebSocket Protocol (2011): rfc-editor.org/rfc/rfc6455
  • WHATWG — Server-Sent Events: html.spec.whatwg.org/multipage/server-sent-events.html
  • Slack RTM protocol: api.slack.com/rtm — reference implementation
  • Discord Gateway v10: discord.com/developers/docs/topics/gateway — real production spec

Practice what you just read

Every foundation concept has a companion quiz to close the loop.