Setting an SSE heartbeat interval: send comments and verify proxy delivery
When a Server-Sent Events (SSE) stream has few real events, an intermediary proxy or network device may close it after an idle timeout. The server can periodically send an SSE comment line such as : keepalive\n\n so bytes flow through the path. Browsers do not dispatch comments as events, but each response buffer must actually flush the data.
When a Server-Sent Events (SSE) stream has few real events, an intermediary proxy or network device may close it after an idle timeout. The server can periodically send an SSE comment line such as : keepalive\n\n so bytes flow through the path. Browsers do not dispatch comments as events, but each response buffer must actually flush the data.
An SSE comment is not an event
An SSE stream is UTF-8 text, and each event block ends with a blank line. A line beginning with a colon is a comment ignored by EventSource. For example, the block below does not call the browser's message handler.
: keepalive
The WHATWG HTML Standard says authors may include a comment about every 15 seconds or so because some legacy proxies drop idle connections. This is an authoring example, not a universally optimal or required interval. Proxies, load balancers, CDNs, NATs, and servers along a user's path can have different idle limits.
Set the interval shorter than the shortest relevant idle timeout, with margin. If the lowest confirmed limit is 60 seconds, testing a 15–20 second heartbeat may be reasonable, but that is an operational choice for that limit, not universal advice. If limits are unknown, inspect each hop's configuration and disconnect logs, then verify a conservative interval shorter than the timeout.
Check transmission, flushing, and buffering together
Writing a comment in application code does not mean it immediately reaches the browser. Application framework buffers, web servers, reverse proxies, and CDNs may accumulate data. Check both when the application produces heartbeats and when the client actually receives them.
NGINX proxy response buffering is enabled by default. Disabling it passes upstream response data to the client as it is received, but does not flush application buffers by itself. Apply suitable settings only to the SSE path and verify application flushing plus NGINX and later hops. An X-Accel-Buffering: no response header can disable buffering when NGINX permits it; it has no effect if proxy configuration ignores that header.
Choose cadence and cost in the deployed path
Find SSE idle timeouts for proxies, load balancers, and gateways between browser and server. An idle timeout is usually different from total connection lifetime.
Choose an interval with margin below the shortest limit. Send comments only when real events have not already produced traffic.
Compare production time and browser receipt time to distinguish generation cadence, buffering, and network delay.
Configure buffering and flushing for this SSE response and verify arrival through each hop in a test environment. Confirm the meaning of settings for deployed versions before changing production.
Detect client disconnects and clean up timers, subscriptions, and work. Each open stream and heartbeat consumes memory, file descriptors, and network traffic.
Heartbeats sent too often keep client/server resources active and increase traffic; sent too rarely they may miss the shortest intermediate idle limit. Balance path limits, observed delivery delay, connection count, and mobile power/data cost. Mobile and proxy paths vary, so review the interval using representative paths and operational metrics.
Keeping a connection alive is not reconnecting
A heartbeat merely sends periodic bytes through the path to reduce the chance of an idle close. It does not guarantee that the connection survives or that events are delivered. The connection can fail between heartbeats; EventSource may attempt to reconnect after a close. Reconnect delay, event IDs, event retention, and recovery from missing or duplicated events require separate design.
Sources and scope
[WHATWG HTML Standard: Server-sent events](https://html.spec.whatwg.org/multipage/server-sent-events.html)
[MDN: Using server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events)
[NGINX: proxy_buffering in ngx_http_proxy_module](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_buffering)
About 15 seconds is an example in WHATWG's authoring guidance. NGINX buffering claims apply to that module and its effective configuration. There is no universal heartbeat interval; no live connection test or load measurement was performed.