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:
- The user sends 1 prompt to the server (standard HTTP POST).
- 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
EventSourceautomatically 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β
| Dimension | Server-Sent Events (SSE) | WebSockets |
|---|---|---|
| Communication Flow | Unidirectional (Server Client) | Bidirectional (Client Server) |
| Protocol Foundation | Standard HTTP/1.1 or HTTP/2 | Custom 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 Workloads | LLM token streaming, stock tickers, news feeds | Multiplayer games, collaborative whiteboards |
π‘ What Interviewers Ask Next (Follow-Up Traps)β
-
"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.
-
"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.
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β
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 Community
Start at #welcome-and-rules, say hello in #general-chat, clear doubts, and collaborate with engineers worldwide.