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:
{ 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-typePolling
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/
doneLong 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:
{ 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/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:
{ 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/eventsThe SSE message format
Four field names, and a blank line ends each event:
{ 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| 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:
{ 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/eventsA 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:
nc -l 9000 | grep -i 'last-event' &
curl -s --max-time 1 -H 'Last-Event-ID: 7' localhost:9000/eventsThat 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:
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'The answer is 101 — the only status that means “stop speaking HTTP”:
{ 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=='A server that required the upgrade and did not get one says so:
{ 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 -2Computing the accept key
Not magic: SHA-1 of the client’s key plus a fixed GUID, base64-encoded:
printf 'dGhlIHNhbXBsZSBub25jZQ==258EAFA5-E914-47DA-95CA-C5AB0DC85B11' \
| openssl dgst -sha1 -binary | openssl base64That 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:
python3 -c "
import sys
payload = b'hi'
sys.stdout.buffer.write(bytes([0x81, len(payload)]) + payload)" | cat -v81 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.