Skip to content
Tutorials Center
My learning

Topics

Lesson 52 of 79 ยท Application Layer: Names and Web

WebSockets, SSE and Real-Time Communication

Compare polling, long polling, Server-Sent Events and WebSockets, learn the WebSocket upgrade handshake and frame format, and see WebRTC in context. Live Node demos.

HTTP was designed around one pattern: the client asks, the server answers. That is perfect for loading a page, but poor for things that happen on the server's schedule: a new chat message, a stock price, a delivery notification, another player's move. The server cannot simply send data to a browser that has not asked for it.

Over the years the web found several ways around this. Some are clever uses of plain HTTP (polling, long polling), and two are purpose-built: Server-Sent Events for one-way streams and WebSockets for full two-way conversations. In this lesson you compare them, run each on your own loopback server, look at the bytes of a WebSocket handshake and frame, and see where WebRTC fits for direct peer-to-peer media.

Polling and long polling

Short polling is the simplest idea: the client asks "anything new?" every few seconds. It works everywhere and needs no special server support. But it is wasteful (most answers are "no") and slow (an event waits for the next poll). Poll more often and you get lower delay but more load, and every request carries full HTTP headers.

Long polling improves it. The client sends a request, and the server holds it open until it has something to say, or until a timeout of perhaps 30 seconds. When the response arrives, the client processes it and immediately sends the next request. Delay is low because a request is always waiting, and there are no empty answers, but each event costs a full request/response, and a server holding thousands of idle requests needs an architecture that does not use one thread per request.

The model below compares the approaches for three events in a 2-second window. It computes requests sent and the average delay between an event and the moment the client hears about it.

Node.js Polling versus long polling versus push

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Polling trades load against delay and cannot win on both. The real fix is to keep one connection open and let the server write to it whenever it likes.

Server-Sent Events

Server-Sent Events (SSE) turn a normal HTTP response into a never-ending stream. The server answers with Content-Type: text/event-stream and does not close the connection. It then writes small text messages whenever it wants. The browser exposes this as the EventSource API, which also reconnects automatically and tells the server the last message it saw.

The format is plain text. A message is a block of field: value lines ending with a blank line:

TEXT
id: 42
event: price
data: {"symbol":"ACME","value":18.4}

  • data: is the payload; several data: lines are joined with newlines.
  • event: names the event type (the default is message).
  • id: sets the last event ID. After a reconnect, the browser sends it back in a Last-Event-ID header so the server can resume.
  • retry: suggests a reconnect delay in milliseconds.
  • A line starting with : is a comment, often used as a keep-alive heartbeat.

SSE is one-way: server to client. The client can still send data with ordinary HTTP requests. Because it is plain HTTP, SSE works with existing proxies, authentication and cookies, and over HTTP/2 many streams share one connection (over HTTP/1.1 browsers allow only about six connections per host, which limits how many SSE streams a page can open).

This demo runs a real SSE server on loopback and parses the stream the way EventSource would.

Node.js A Server-Sent Events stream

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

WebSockets

When both sides need to send at any time (chat, multiplayer games, collaborative editing), you want a true two-way channel. WebSocket (RFC 6455) provides it: a single long-lived TCP connection, started as an HTTP request and then upgraded to a different protocol with its own lightweight framing. Either side can send messages at any moment, with only a few bytes of overhead per message.

The upgrade handshake

A WebSocket begins as an ordinary HTTP/1.1 GET with special headers:

HTTP
GET /chat HTTP/1.1
Host: example.org
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13

If the server agrees, it replies with status 101 Switching Protocols:

HTTP
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=

After the blank line the same TCP connection no longer speaks HTTP. It carries WebSocket frames in both directions.

sequenceDiagram
    participant C as Browser
    participant S as Server
    C->>S: GET /chat, Upgrade: websocket, Sec-WebSocket-Key
    S-->>C: 101 Switching Protocols, Sec-WebSocket-Accept
    Note over C,S: same TCP connection, now WebSocket frames
    C->>S: text frame "hello"
    S-->>C: text frame "HELLO"
    S-->>C: text frame (server-initiated)
    C->>S: close frame
    S-->>C: close frame

Only the first exchange is HTTP. After the 101 response both sides send frames whenever they like.

The Sec-WebSocket-Accept value proves that the server really understands WebSockets and that a cache or plain HTTP server did not just echo old data. The server appends a fixed GUID from the standard to the client's key, takes the SHA-1 hash, and Base64-encodes it. You can compute it yourself:

Node.js Compute Sec-WebSocket-Accept and build frames

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Frames

Everything after the handshake is a sequence of frames. The header is tiny:

Part Meaning
FIN bit 1 if this is the last frame of a message (large messages can be split into fragments).
Opcode (4 bits) 0x1 text (UTF-8), 0x2 binary, 0x0 continuation, 0x8 close, 0x9 ping, 0xA pong.
MASK bit 1 if the payload is masked.
Payload length 7 bits for lengths up to 125. The value 126 means a 16-bit length follows, 127 means a 64-bit length.
Masking key 4 bytes, present only when MASK = 1.
Payload The data, XORed byte by byte with the key if masked.

Look at the output above: the unmasked frame 81 03 486921 is five bytes (0x81 = FIN + text, 0x03 = length 3, then the characters). Compare with the HTTP headers you would send for the same message. All frames from client to server must be masked with a fresh random key. This is not for secrecy (the key travels with the frame) but to stop a malicious script from crafting bytes that confuse an old caching proxy into treating them as HTTP. Server-to-client frames are not masked.

Ping and pong frames act as heartbeats to detect dead connections and keep NAT and proxy timeouts from closing an idle link. A normal shutdown uses a close frame with a status code, answered by a close frame from the other side.

Here is a complete WebSocket conversation on loopback. The server is a deliberately minimal implementation (text frames shorter than 126 bytes, no fragmentation) that takes over the socket on the HTTP upgrade event. The client is the built-in WebSocket class, the same API browsers have.

Node.js A WebSocket echo over loopback

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

A real server library also handles fragmented messages, long payloads, ping/pong, close codes, per-message compression and many connections. Use one rather than writing your own.

Practical concerns

  • Secure WebSockets use the scheme wss://, which is the same protocol inside TLS, and should be your default. Plain ws:// can be altered by proxies.
  • Authentication. Browsers send cookies with the handshake, but a WebSocket is not protected by the usual same-origin rules: the server should check the Origin header to prevent cross-site hijacking.
  • Scaling. Each client holds a connection, so servers must handle many thousands of mostly idle sockets. A load balancer needs to support the upgrade and long timeouts, and messages between users on different servers need a shared message bus.
  • Reconnection. Connections drop on mobile networks. Clients need automatic reconnect with back-off, and a way to resync missed messages (unlike SSE, WebSocket has no built-in resume).
  • HTTP/2 and HTTP/3. WebSockets can be carried over multiplexed connections with an extended CONNECT method, but the classic HTTP/1.1 upgrade is still the most common.

Which one to choose

Need Good choice
Rarely changing data, simplicity Short polling
Server pushes updates, client only listens (feeds, notifications, progress bars, live scores) SSE
Frequent two-way messages (chat, games, collaborative editing) WebSocket
Binary data in both directions at low overhead WebSocket
Works through the strictest proxies Long polling or SSE

Start with the simplest option that meets the need. SSE is often enough and is much easier to run than WebSockets.

WebRTC in brief

WebRTC (Web Real-Time Communication) is a different kind of tool: it lets browsers exchange audio, video and arbitrary data directly with each other, with very low delay. Video calls in the browser are built on it. It is not a single protocol but a bundle:

  • Signaling is how two peers find each other and exchange connection details. WebRTC deliberately does not define it: you use a WebSocket or HTTP server of your own to swap messages (the offer, the answer and candidate addresses, in a format called SDP).
  • ICE, STUN and TURN solve the NAT problem. A STUN server tells a peer its public address and port; ICE tries the possible address pairs to find a working path; a TURN server relays the traffic when no direct path works (for example behind symmetric NAT).
  • DTLS and SRTP encrypt data and media. Everything in WebRTC is encrypted.
  • Media (audio and video) is sent over UDP with RTP, because a late video frame is useless and retransmitting it would be worse than skipping it. Data channels carry arbitrary messages over SCTP on top of DTLS, with reliable or unreliable modes.

So a typical call uses a WebSocket for signaling, and then the media flows peer to peer over UDP, or via a TURN relay if it must.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the main difference between short polling and long polling?

    Long polling keeps a request waiting on the server, so events are delivered immediately, and the client re-requests after each answer. Short polling gets an immediate (often empty) answer and asks again later.
  2. 2. Which technology is one-way (server to client) and reconnects automatically in the browser?

    SSE uses a long-lived HTTP response with text/event-stream, and EventSource reconnects and sends Last-Event-ID.
  3. 3. What HTTP status code does a server send to accept a WebSocket upgrade?

    The 101 response signals that the connection now speaks another protocol. The Sec-WebSocket-Accept header completes the handshake.
  4. 4. Why must client-to-server WebSocket frames be masked?

    The masking key is sent with the frame, so it gives no secrecy. Its purpose is to prevent cache-poisoning tricks against badly written proxies.
  5. 5. In a WebRTC video call, what is the signaling server used for?

    WebRTC does not define signaling. You use your own server, often via WebSocket, to exchange the setup information; media then flows peer to peer or through TURN.

Summary

  • Plain HTTP cannot push data. Short polling wastes requests, and long polling holds requests open to cut the delay.
  • Server-Sent Events stream text/event-stream messages (data, event, id) one-way from server to client, with automatic reconnection over ordinary HTTP.
  • WebSocket upgrades an HTTP connection with 101 Switching Protocols and Sec-WebSocket-Accept (SHA-1 of the key plus a fixed GUID, Base64-encoded), then exchanges small frames in both directions.
  • Frames have FIN, opcode (text, binary, ping, pong, close), length and a mask; client frames are always masked. Use wss:// and check Origin.
  • Pick the simplest tool: SSE for one-way updates, WebSocket for two-way traffic.
  • WebRTC connects browsers directly for media and data, using your signaling server, ICE/STUN/TURN for NAT traversal, and encrypted UDP-based transport.

Next, see the oldest real-time-ish system of all, store-and-forward messaging, in Email: SMTP, IMAP and POP3.