Real time: WebSocket, SSE and polling

Pushing information to the browser: polling, long polling, Server-Sent Events and WebSocket, what they cost and how to keep them up at scale.

  • 41 cards
  • 4 topics
  • Free

The outline, topic by topic

Each topic is weighted the way the exam weighs it.

  1. Polling and long polling
  2. Server-Sent Events
  3. WebSocket
  4. Holding up at scale

A few cards from the deck

  • What is polling?

    Show the answer

    The client asks again at a regular interval whether there is anything new.

    Simple, but it wastes requests and delays information until the next ask.

  • A dashboard shows live prices; the browser sends nothing. The simplest, sturdiest choice?

    • One e-mail per update
    • Server-Sent Events
    • A WebSocket
    • Polling every second
    Show the answer

    Server-Sent Events

    The flow only goes from server to client: SSE is enough, in plain HTTP, with reconnection built in.

  • SSE or WebSocket?

    Show the answer

    SSE: plain HTTP, built-in reconnection, but server to client only, in text. WebSocket: bidirectional and binary, but reconnection and heartbeats are yours to build.

    SSE also runs over HTTP/2, where several streams share one connection; over HTTP/1.1, browsers cap it at about 6 streams per domain.

  • Opening a WebSocket: after “GET with Upgrade: websocket and Sec-WebSocket-Key”, what is the next message?

    • 101 Switching Protocols with Sec-WebSocket-Accept
    • Text message from the browser
    • Message pushed by the server
    • Ping
    Show the answer

    101 Switching Protocols with Sec-WebSocket-Accept

    The server accepts: the same TCP connection becomes a WebSocket. → WebSocket open