Skip to content
Sign inGet all access
Chapter 8·14 minFree

Streaming and Persistent Connections

Long polling, Server-Sent Events, and the WebSocket upgrade.

A request gets one response. Sometimes the server has more to say.

The problem

One request, one response, connection idle. A notification does not fit that shape. Three answers follow; this header picks the middle one:

terminal
{ printf 'HTTP/1.1 200 OK\r\n'
  printf 'Content-Type: text/event-stream\r\n'
  printf 'Connection: close\r\n\r\ndata: x\n\n'
} | nc -l 9000 >/dev/null &
curl -sI localhost:9000/events | grep -i content-type
OutputContent-Type: text/event-stream

Polling

Ask again on a timer — the baseline everything else improves on:

for i in 1 2 3; do
  curl -s -o /dev/null -w '%{http_code}\n' localhost:8080/
done
Output200200200

Long polling holds the request open until there is news. Still one response per message, but it arrives immediately.

Streaming a response

Chunked encoding from chapter 5, for real. -N stops curl buffering:

terminal
{ printf 'HTTP/1.1 200 OK\r\n'
  printf 'Transfer-Encoding: chunked\r\n'
  printf 'Connection: close\r\n\r\n'
  printf '6\r\nfirst\n\r\n'; sleep 0.3
  printf '7\r\nsecond\n\r\n'
  printf '0\r\n\r\n'
} | nc -l 9000 >/dev/null &
curl -sN localhost:9000/
Outputfirstsecond

The lines arrived 300ms apart on one connection — one response taking its time.

Server-Sent Events

A Content-Type, and a body that never ends:

terminal
{ printf 'HTTP/1.1 200 OK\r\n'
  printf 'Content-Type: text/event-stream\r\n'
  printf 'Cache-Control: no-cache\r\n'
  printf 'Connection: close\r\n\r\n'
  printf 'data: first\n\n'
  printf 'data: second\n\n'
} | nc -l 9000 >/dev/null &
curl -sN localhost:9000/events
Outputdata: firstdata: second

The SSE message format

Four field names, and a blank line ends each event:

terminal
{ printf 'HTTP/1.1 200 OK\r\n'
  printf 'Content-Type: text/event-stream\r\n'
  printf 'Connection: close\r\n\r\n'
  printf 'data: first\n\n'
  printf 'data: second\n\n'
  printf 'event: ping\ndata: 42\nid: 7\n\n'
} | nc -l 9000 >/dev/null &
curl -sN localhost:9000/events
Outputdata: firstdata: secondevent: pingdata: 42id: 7
Field Does
data: the payload, repeatable for multi-line
event: a name the client can listen for
id: the last-seen position, for resuming
retry: how long to wait before reconnecting

Repeated data: lines join into one payload; a line starting : is a comment:

terminal
{ printf 'HTTP/1.1 200 OK\r\n'
  printf 'Content-Type: text/event-stream\r\n'
  printf 'Connection: close\r\n\r\n'
  printf ': keepalive\n\n'
  printf 'retry: 5000\n\n'
  printf 'data: line one\ndata: line two\n\n'
} | nc -l 9000 >/dev/null &
curl -sN localhost:9000/events
Output: keepaliveretry: 5000data: line onedata: line two

A comment keeps an idle connection alive through a proxy that would time it out.

Reconnection

The browser reconnects and sends back the last id: it saw:

terminal
nc -l 9000 | grep -i 'last-event' &
curl -s --max-time 1 -H 'Last-Event-ID: 7' localhost:9000/events
OutputLast-Event-ID: 7

That is the whole resume protocol, and EventSource does it for free. The server decides what “everything after 7” means.

The WebSocket upgrade

A normal GET with four headers asking to change protocol:

terminal
nc -l 9000 | tr -d '\r' &
curl -s --max-time 1 localhost:9000/ws \
  -H 'Upgrade: websocket' -H 'Connection: Upgrade' \
  -H 'Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==' \
  -H 'Sec-WebSocket-Version: 13'
OutputGET /ws HTTP/1.1Host: localhost:9000User-Agent: curl/8.7.1Accept: */*Upgrade: websocketConnection: UpgradeSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==Sec-WebSocket-Version: 13

The answer is 101 — the only status that means “stop speaking HTTP”:

terminal
{ printf 'HTTP/1.1 101 Switching Protocols\r\n'
  printf 'Upgrade: websocket\r\n'
  printf 'Connection: Upgrade\r\n'
  printf 'Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n'
} | nc -l 9000 >/dev/null &
curl -sI localhost:9000/ws -H 'Upgrade: websocket' \
  -H 'Connection: Upgrade' \
  -H 'Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ=='
OutputHTTP/1.1 101 Switching ProtocolsUpgrade: websocketConnection: UpgradeSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=

A server that required the upgrade and did not get one says so:

terminal
{ printf 'HTTP/1.1 426 Upgrade Required\r\n'
  printf 'Upgrade: websocket\r\n'
  printf 'Content-Length: 0\r\n\r\n'
} | nc -l 9000 >/dev/null &
curl -sI localhost:9000/ws | head -2
OutputHTTP/1.1 426 Upgrade RequiredUpgrade: websocket

Computing the accept key

Not magic: SHA-1 of the client’s key plus a fixed GUID, base64-encoded:

terminal
printf 'dGhlIHNhbXBsZSBub25jZQ==258EAFA5-E914-47DA-95CA-C5AB0DC85B11' \
  | openssl dgst -sha1 -binary | openssl base64
Outputs3pPLMBiTxaQ9kYGzzhZRbK+xOo=

That matches the header above, and RFC 6455’s worked example.

Where HTTP stops

After the 101 the bytes are frames — a two-byte header and a payload:

terminal
python3 -c "
import sys
payload = b'hi'
sys.stdout.buffer.write(bytes([0x81, len(payload)]) + payload)" | cat -v
OutputM-^A^Bhi

81 is final-frame-text, 02 the length, hi the payload. Everything after is binary.

SSE or WebSockets?

SSE WebSockets
Direction server to client both
Protocol HTTP, text a second protocol
Reconnect automatic you write it
Through proxies usually, unless buffered needs Upgrade support
Binary no yes

Default to SSE: one header, plain text, and the browser reconnects for you.

Reach for WebSockets when the client must also send continuously — a game, a cursor, a collaborative editor. Anything less is a second protocol for nothing.