SkillSprout
Chapter 28 · Lesson 2
Intermediate
3 min read

WebSockets, Server-Sent Events and Choosing

Overview

Long polling still pays for a new request on every message. WebSockets and Server-Sent Events keep one connection open so the server can push data whenever it wants. They differ in direction: WebSockets are two-way, SSE is one-way from server to client.

Detailed Notes

What are WebSockets and SSE

WebSocket: a persistent, two-way connection. The client starts with a normal HTTP request asking to "upgrade", and after that both sides can send small messages at any time over the same connection, with very little overhead per message.

Server-Sent Events (SSE): a persistent one-way stream from server to client over plain HTTP. The server keeps the response open and writes events to it as they happen. The browser has built-in support that reconnects automatically.

The problem they solve

Polling pays a full request for every update and adds delay. Chat, multiplayer games, live dashboards and stock tickers need low delay and frequent messages. Keeping one connection open removes the repeated request overhead and lets the server push instantly.

How it works

WebSocket

  • Connection is long-lived, so the server must track which user is connected to which server.
  • Needs heartbeats (small periodic ping messages) so dead connections are noticed and idle timeouts in proxies do not cut it.
  • Some proxies and firewalls interfere with the upgrade.
  • Use wss:// (encrypted) and authenticate during the handshake.

SSE

  • Only server to client; the client sends data with ordinary HTTP requests.
  • Each event can carry an ID. After a drop, the browser reconnects and sends Last-Event-ID, so the server can resend what was missed.
  • Plain HTTP means it works with existing proxies, caching rules and authentication.

Choosing

Long pollingSSEWebSocket
DirectionClient asks, server answersServer to clientTwo-way
ConnectionNew request each timeOne open streamOne open connection
Auto-reconnectYou build itBuilt into browsersYou build it
Best forRare updates, strict networksFeeds, notifications, live scoresChat, games, collaboration

A simple example

A live cricket score page: only the server has news, so SSE fits. A chat app: both sides send often, so WebSocket fits. A small admin tool that is rarely updated and sits behind an old proxy: long polling is enough.

System design example

Millions of connected users cannot all be on one server, so you run many gateway servers, each holding a share of connections. When user A sends a chat message, A's gateway publishes it to a pub/sub layer (covered later in the Messaging chapter) and also saves it to a database. The gateway holding user B's connection receives it and pushes it down B's socket instantly. A load balancer spreads new connections across gateways. If B disconnects, B reconnects and asks for messages after the last ID it saw, which the database supplies. Gateways stay thin, with durable data kept outside them, so losing a gateway only drops connections that then reconnect elsewhere.

Summary

  • WebSocket: two-way, low overhead; best for chat, games, collaboration.
  • SSE: one-way server push over HTTP with built-in reconnect; best for feeds and notifications.
  • Long polling: simplest fallback that works everywhere.
  • Always design heartbeats and resume-from-last-ID for dropped connections.
  • Scale with gateway servers plus a message routing layer, not one big server.

Architecture Diagram

WebSocket chat architecture
Architecture
Clients
Load Balancer
WS Gateways
Pub/Sub
Message Store

Gateways hold connections; pub/sub routes messages between them.

  1. 1The client opens a wss connection; the load balancer routes it to a gateway.
  2. 2The client sends a message to its gateway.
  3. 3The gateway publishes it to pub/sub and writes it to the store.
  4. 4Pub/sub delivers it to the gateway holding the recipient's connection.
  5. 5That gateway pushes it to the recipient instantly.

Common Interview Questions

Chapter quiz

Long Polling vs WebSocket vs SSE · 5 questions

  1. 1.Why can't a server in plain HTTP just send updates whenever it wants?

  2. 2.In long polling, what does the server do when there is no new data?

  3. 3.Which technique is two-way over a single persistent connection?

  4. 4.A live sports score page only pushes data from server to browser. Best fit?

  5. 5.What lets a client recover missed events after reconnecting?

0 of 5 answered

Finished this lesson?

0 of 91 lessons done · 0% of the course