LESSON 02 · Client and server

Choose the live transport

Compare polling, long polling, server-sent events, and WebSocket against update frequency and connection cost.

SYSTEM VIEW

The message path

Diagram view
Requests—
Held open—
Direction—

Watch Ben’s laptop wait for Ana’s message four ways, from asking again and again to one open two-way channel.

  1. With short polling, Ben’s laptop asks the Chat service for updates every 5 s, and while nothing is new each reply comes back empty. Ana sends “lunch?” over HTTP, and the laptop sees it only when its next poll asks.

    Polls
    every 5 s
    While quiet
    empty replies

    What changed

    • A poll every 5 s
    • Empty replies while quiet
    • Next poll gets “lunch?”
  2. Scale it up: 1 million open apps, each polling every 5 s, send the Chat service 200,000 requests a second. While traffic is quiet, most come back empty, yet each one still costs work.

    Open apps
    1 million
    Poll every
    5 s

    What changed

    • 1 million open apps
    • 200,000 requests/s
    • Most replies empty
  3. With long polling, Ben’s laptop sends one request and the Chat service holds it until an event or a timeout. Ana’s “lunch?” answers it at once, and the laptop opens the next wait. Fewer empty replies, but reconnects and timeouts still need handling.

    Held until
    event or timeout
    Example
    Telegram’s getUpdates

    What changed

    • GET held open
    • “lunch?” answers it at once
    • Laptop opens the next wait
  4. With server-sent events, Ben’s laptop opens one GET /events and the response stays open. “lunch?” streams down it as an event, one way only, so sends such as Ana’s go over plain HTTP. EventSource reconnects with the last event ID it saw.

    Browser API
    EventSource
    Suits
    one-way feeds

    What changed

    • GET /events held open
    • “lunch?” streamed as an event
    • Sends stay plain HTTP
  5. A WebSocket starts as an HTTP request. Ben’s laptop asks the Gateway to upgrade, the Gateway answers 101 Switching Protocols, and the same connection stays open as one two-way socket.

    Starts as
    an HTTP request
    Answer
    101 Switching Protocols

    What changed

    • Gateway
    • Upgrade: websocket
    • 1 socket held open
  6. Ana’s “lunch?” reaches the Chat service, which passes it to the Gateway, and the Gateway sends it down Ben’s open socket. Ben answers “yes!” up the same socket, with no new request. It suits frequent, asynchronous chat updates.

    Channel
    one, both ways
    Suits
    frequent chat updates

    What changed

    • “lunch?” down the socket
    • “yes!” up the same socket
  7. The Gateway pings and Ben’s laptop answers pong: a heartbeat proving the peer is still there. The server also handles reconnects, backpressure and tokens that expire while the socket stays open. The socket is a delivery path, not storage.

    Heartbeat
    ping, pong
    Not
    durable storage

    What changed

    • Heartbeats
    • Reconnects, backpressure, token refresh
    • A path, not storage
  8. Use the simplest transport that meets your latency and scale goals. Try each: a game lobby updating every second keeps one WebSocket open both ways; a support inbox updating every few hours can start with long polling, polling or SSE.

    This chapter
    WebSocket for chat
    Account APIs
    plain HTTP

    What changed

    • Lobby: one WebSocket
    • Inbox: long polling
    • Account APIs stay on HTTP
    Updates arrive

Space plays · ← → step · Home resets

THE IDEA

How it works

Ordinary HTTP works well for login and profile changes and can also send a message. For live delivery, short polling repeatedly asks for updates, creating empty requests when traffic is quiet: one million open apps polling every 5 seconds make 200,000 requests per second. Long polling waits for an event or timeout before the client opens another request, as Telegram’s Bot API does with getUpdates. It reduces empty replies but still needs reconnect and timeout handling.

Server-sent events (SSE) keep one HTTP response open and stream events from server to client. Browsers support them through EventSource, which reconnects automatically and reports the last event ID it saw. They suit one-way feeds, such as the token streams from LLM APIs. Chat can use them too, with sends going over plain HTTP requests.

A WebSocket starts with a client handshake, an HTTP request asking to upgrade, and then holds one bidirectional channel carrying messages both ways. It suits frequent asynchronous chat updates, but the server must manage many live connections, heartbeats (small periodic messages proving the peer is still there), reconnects, backpressure (slowing output a client cannot absorb), and refreshing credentials that expire while the socket stays open. A persistent channel is a delivery path, not durable storage.

Use the simplest transport that meets the product’s latency and scale goals. This chapter uses WebSocket for live send and receive while keeping routine account APIs on HTTP. Mobile push wakes an inactive device; it is a notification path and may be delayed or disabled.

DECISION POINT

A support inbox gets one update every few hours, while a game lobby exchanges updates each second. Would both need WebSocket?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • Polling repeats requests, long polling holds them, SSE streams one way, and WebSocket keeps a two-way channel.
  • Choose transport separately from persistence and push notification.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
RecallQUESTION 01

What is the distinctive cost of short polling for a mostly quiet inbox?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.