Skip to main content

Why ChatGPT Uses SSE Instead of WebSockets

🎯 The Question​

"When ChatGPT streams tokens to your screen in real time, why does OpenAI use Server-Sent Events (SSE) instead of bidirectional WebSockets?"


⚑ 30-Second Elevator Pitch​

WebSockets are designed for bidirectional, full-duplex communication (like multiplayer games or live chat rooms where both client and server constantly send messages simultaneously).

LLM token streaming is strictly unidirectional:

  1. The user sends 1 prompt to the server (standard HTTP POST).
  2. The AI model streams hundreds of tokens back sequentially over time.

Why SSE is superior for LLM streaming:

  • Runs over standard HTTP/2: Free HTTP/2 connection multiplexing, header compression, and zero custom TCP upgrade handshakes.
  • Built-in Browser Reconnection: Native EventSource automatically reconnects and resumes streams if Wi-Fi drops.
  • Firewall & Proxy Friendly: Uses standard HTTP/HTTPS (text/event-stream), avoiding WebSocket proxy blocking in corporate enterprise firewalls.

🧠 Under-the-Hood: SSE Stream vs. WebSocket Protocol​


πŸ”¬ How SSE Works Over HTTP​

The server simply responds with standard HTTP headers:

HTTP/1.1 200 OK
Content-Type: text/event-stream
Cache-Control: no-cache
Connection: keep-alive

data: {"token": "Hello"}

data: {"token": " world"}

data: [DONE]

The browser receives tokens as continuous text chunks without parsing custom binary framing protocols.


πŸ“Œ Comparison Matrix: Server-Sent Events (SSE) vs. WebSockets​

DimensionServer-Sent Events (SSE)WebSockets
Communication FlowUnidirectional (Server o o Client)Bidirectional (Client ↔\leftrightarrow Server)
Protocol FoundationStandard HTTP/1.1 or HTTP/2Custom ws:// / wss:// TCP protocol
Auto-Reconnectionβœ… Built-in by browser specification❌ Requires custom JavaScript heartbeat logic
Corporate Firewallsβœ… Passes cleanly (Standard HTTPS 443)⚠️ Often blocked/dropped by enterprise proxies
Ideal WorkloadsLLM token streaming, stock tickers, news feedsMultiplayer games, collaborative whiteboards

πŸ’‘ What Interviewers Ask Next (Follow-Up Traps)​

  1. "What is the 6-connection limit bug with SSE in HTTP/1.1?"

    • Answer: Under HTTP/1.1, browsers restrict maximum connections per domain to 6. Opening 6 SSE streams in different tabs completely blocks all subsequent HTTP requests. Solution: Serve SSE over HTTP/2, which multiplexes hundreds of streams over a single shared TCP connection.
  2. "Can the client send data over an open SSE connection?"

    • Answer: No. SSE is strictly unidirectional. If the client needs to send new data (e.g. stop generating or send a new prompt), it makes a separate standard HTTP POST request.

Placement & Interview Takeaway

Interview Answer: ChatGPT uses Server-Sent Events (SSE) because LLM inference is strictly unidirectional (client sends a single prompt, server streams tokens back). SSE runs over standard HTTP/2 with built-in auto-reconnection and seamless firewall traversal without the overhead of WebSocket state management.


πŸ“Ί Video Explanation​

πŸš€ Level Up Your Engineering Mindset

Never Study in Isolation. Join Binary Dose.

Connect with fellow engineers, clear conceptual doubts, and get 2 visual breakdowns & engineering mental models daily across our community channels.

Discord
Official Hub

Discord Community

Start at #welcome-and-rules, say hello in #general-chat, clear doubts, and collaborate with engineers worldwide.

Join Discord Server→
⚑
Daily Knowledge Drops

Daily Visual Deep-Dives

Bite-sized system design schematics, core OS internals, and DSA intuition served right into your feeds every single day.

πŸ’‘Have handwritten notes, cheat-sheets, or want to publish a technical article?
Learn How to Contribute→