Server-sent events and buffering
SSE is still an HTTP response
Server-sent events (SSE) suit status updates, progress and notifications where the server sends text messages to the browser. The browser uses EventSource; client commands use other requests. Unlike WebSocket, SSE does not require an upgrade handshake. A stream uses text/event-stream and UTF-8. A blank line completes an event; network chunks need not align with event boundaries.
id: 1data: ready
id: 2data: packed
id: 3data: shippedA disconnected client can reconnect with its last received event ID. The server must decide how to replay or recover missing updates; an ID alone does not provide durable delivery. In the fixture, a request with Last-Event-ID: 2 returns only shipped; a completed cursor returns 204, telling a conforming EventSource not to reconnect. The browser client can also explicitly close its EventSource.
const stream = new EventSource("/events");stream.onmessage = event => { console.log(event.lastEventId, event.data); if (event.data === "shipped") stream.close();};stream.onerror = () => console.log("stream interrupted");Buffering trades throughput for delivery latency
Nginx normally buffers upstream HTTP response data. For an event stream, holding bytes can delay a small update. Turning off response buffering on this specific route makes Nginx relay bytes as they arrive, but application servers, compression, intermediaries and client libraries can still delay visibility. The origin must write events incrementally. Request buffering is a different setting controlling client request bodies.
location = /events { proxy_pass http://127.0.0.1:19040; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_buffering off; proxy_cache off; gzip off; proxy_read_timeout 5s;}The upstream can also request buffering changes with X-Accel-Buffering: no, unless Nginx is configured to ignore that response field. The lab selects behavior explicitly and sets Cache-Control: no-store at the origin. Do not blanket-disable buffering on ordinary pages: doing so can tie upstream progress to a slow client.
curl --noproxy "*" -N http://127.0.0.1:18310/eventscurl --noproxy "*" -N -H "Last-Event-ID: 2" http://127.0.0.1:18310/eventsExpected first stream: three updates in order, written roughly a quarter second apart by the fixture. The second stream contains only shipped. This is a timing expectation, not a measured latency guarantee. curl -N changes client output buffering, not Nginx settings. A complete body comparison cannot establish that updates arrived incrementally; record event arrival times separately.
Practice
Explain why SSE needs no upgrade header and why a final newline matters. Compare a resumed stream against a new request. Source: https://html.spec.whatwg.org/multipage/server-sent-events.html
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Server-sent events and buffering
Deliver one-way HTTP event streams without unnecessary buffering and understand reconnection and replay.
Which response media type identifies SSE?
What terminates an SSE event?
Which setting controls upstream response buffering?
What should the fixture return for Last-Event-ID: 2?
What can a 204 response tell EventSource?