Server-Sent Events (SSE) vs WebSockets over HTTP/2: Choosing the Right Real-Time Streaming Architecture
Why HTTP/2 multiplexing made Server-Sent Events the modern standard for LLM token streams and live telemetry over complex WebSockets.
Real-time data streaming is essential for modern web applications-from live financial dashboards and notification hubs to streaming AI token generation. For years, WebSockets were chosen reflexively for all real-time requirements despite protocol complexity, lack of automatic reconnection, and firewall traversal friction. Under HTTP/2, Server-Sent Events (SSE) overcome historical connection limits through multiplexing, offering a lightweight, unidirectional alternative that rides standard HTTP.
THE PRODUCTION PROBLEM STATEMENT
Engineers default to WebSockets for simple server-to-client updates, incurring heavy infrastructure costs for connection brokers, custom heartbeat pings, and proxy configuration headaches.
SSE vs WebSockets: Architectural Matrix
WebSockets establish a full-duplex, bidirectional TCP connection outside the standard HTTP lifecycle. They are essential for low-latency, two-way communication (like multiplayer gaming or collaborative drawing canvases).
Server-Sent Events (SSE) operate over standard HTTP (`Content-Type: text/event-stream`). The client uses native `EventSource`, which includes built-in exponential reconnects, last-event-id tracking, and native proxy caching compatibility. For 90% of web apps (where data flows primarily from server to client), SSE is significantly simpler and more reliable.
export function subscribeToLiveMetrics(onData: (metric: any) => void) {
const eventSource = new EventSource('/api/telemetry/stream');
eventSource.onmessage = (event) => {
const parsed = JSON.parse(event.data);
onData(parsed);
};
eventSource.onerror = (err) => {
console.warn('[SSE Connection interrupted - browser will reconnect automatically]', err);
};
// Cleanup on unmount
return () => {
eventSource.close();
};
}[PROBABLE CAUSE] A reverse proxy (such as Nginx or Cloudflare) has response buffering enabled.
[VERIFIED RESOLUTION] Set the HTTP header `X-Accel-Buffering: no` in your server response to disable buffering in Nginx and proxies.
Frequently Asked Questions & Technical Inquiries
Direct, practical answers to common architectural and debugging questions.
Yes! Under HTTP/1.1, browsers limited domains to 6 persistent connections. Under HTTP/2, all SSE streams share a single multiplexed TCP connection, completely eliminating connection exhaustion.
Faisal Askari
Verified AuthorSoftware Engineer & Full-Stack Developer
Faisal Askari is a software engineer with deep expertise in web systems architecture, the JavaScript runtime ecosystem, and client-side performance engineering. Over years of hands-on production engineering, he has designed and shipped enterprise dashboards, high-converting digital interfaces, and specialized full-stack applications.
Resilient Full-Stack Architecture: Idempotent APIs, Optimistic UI, and Cache Invalidation
Engineering robust distributed state between client and server: idempotency keys, safe optimistic rollbacks, exponential backoff with jitter, and cache coherence.
Definitive Web Authentication: Secure HttpOnly Session Cookies vs LocalStorage JWT Storage
Why storing access tokens in localStorage exposes users to token exfiltration, and how to structure secure, revocable HttpOnly cookie sessions.