SSE arrives in one chunk instead of a stream
Server-Sent Events land all at once instead of streaming. Three layers act on them: proxy_buffering, gzip and the app itself. How to test each.
What it means
The server writes events one at a time; the browser gets thirty seconds of
nothing, then twenty message events at once. If curl -N straight to the
application streams steadily, something between the app and the browser holds
the bytes. Almost always that is nginx.
How to fix it
Three layers can affect a stream. Check them in order.
Layer 1 is nginx. proxy_buffering is on by default, so nginx reads the
backend's response into buffers and feeds the client at its own pace.
location /events {
proxy_pass http://app;
proxy_http_version 1.1;
proxy_set_header Connection '';
proxy_buffering off;
proxy_cache off;
proxy_read_timeout 1h;
}
proxy_buffering offis the fix. With it, each chunk goes to the client at once.proxy_cache offcancels a cache inherited from an outer block.proxy_read_timeoutdefaults to60sbetween two reads; a quiet stream dies at the minute mark (separate guide).proxy_http_version 1.1andConnection ''do not affect buffering. They let anupstream … keepalivepool reuse connections (RFC 9112 § 9.3). Without them, nginx before 1.29.7 proxies over HTTP/1.0 and sendsConnection: closeby default.
Cannot edit the config? Send a header from the application.
X-Accel-Buffering: no
nginx turns buffering off for that response. The header is nginx-specific;
proxy_ignore_headers X-Accel-Buffering disables it.
Layer 2 is compression
(Content-Encoding).
nginx gzips only the types in gzip_types; the default is text/html. If
someone added text/event-stream (or *), gzip holds bytes until it has a
block to emit. A type cannot be removed from the list, so turn gzip off in the
SSE location.
gzip off; # inside the location that serves the stream
Layer 3 is the application. Many frameworks hold the response until the handler returns. Send the headers first, then write each event as it happens.
// Node.js
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'X-Accel-Buffering': 'no',
});
res.flushHeaders();
function send(data) {
res.write(`data: ${JSON.stringify(data)}\n\n`);
}
A blank line ends an event, and with one \n the browser keeps waiting. Node does
not buffer res.write; Express's compression middleware does. Skip it for
this route or call res.flush() after every event.
# Flask
return Response(
generate(),
mimetype="text/event-stream",
headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},
)
generate() must yield each complete event ("data: …\n\n") as it happens,
not build a list first.
Gunicorn's sync worker serves one connection at a time and kills a request
after --timeout (30 s by default). Use gevent or eventlet workers, or an
ASGI app under uvicorn.
Finding out which layer it is
# straight to the application, bypassing nginx
curl -N http://127.0.0.1:8000/events
# through nginx
curl -N https://example.com/events
- If the first is steady and the second batched, it is nginx.
- If both are batched, it is the application.
- If both are steady and the browser still waits, check
Content-Type.EventSourceneeds the media typetext/event-stream(acharsetparameter is fine); on any other it fireserrorand closes without reconnecting.
See it on your own traffic
Solpuga records each event of a text/event-stream response with its arrival
time. If events arrive spread out with gaps, all is well. If they all arrive at
once after a long pause, something on the way collected them. Same answer as
the two curl runs. More in
Capture & Inspect Traffic.
Tool for this page
SSE stream parserPaste a raw Server-Sent Events stream and see the events, ids, retry hints and framing mistakes.Related
- The stream dies after 60 seconds: proxy_read_timeoutA stream that dies on the same second is a timeout. Default values in nginx, ALB, Cloudflare, Heroku and Envoy, and why a heartbeat is safer.
- Replacement characters «�» in a stream: TextDecoder and the stream flagA multi-byte character was cut at a chunk boundary. One decoder and one flag fix it, and the same mistake is hiding one level up, in the SSE parsing.