Lightweight, HTTP-based unidirectional streaming. Subscribe to live server notifications, background task updates, and automated heartbeats using the native browser EventSource API over standard HTTP/1.1 or HTTP/2 without custom socket libraries.
The stream endpoint sends the following HTTP response headers to keep connections persistently open:
| Header | Value | Purpose |
|---|---|---|
| Content-Type | text/event-stream | Identifies payload as EventSource stream format. |
| Cache-Control | no-cache, no-transform | Prevents intermediate proxies and CDNs from buffering chunks. |
| Connection | keep-alive | Maintains persistent long-lived TCP connection. |
| X-Accel-Buffering | no | Instructs Nginx reverse proxies to stream chunks immediately. |
Production examples for native browser EventSource, React hooks, and cURL:
When to choose Server-Sent Events over WebSockets for application architecture:
| Capability | Server-Sent Events (SSE) | WebSockets (WS) |
|---|---|---|
| Communication Direction | Unidirectional (Server → Client) | Full-duplex bidirectional |
| Transport Layer | Standard HTTP/1.1 or HTTP/2 | TCP upgrade to WebSocket protocol |
| Automatic Reconnection | Native browser auto-retry | Custom application retry required |
| Firewall / Proxy Traversal | Seamless (Standard HTTP GET) | Requires WebSocket proxy support |