Backendcore
Streaming over SSE vs WebSockets
SSE for one-way token streams over plain HTTP; WebSockets when both sides talk continuously.
What it is and why it exists
Server-Sent Events: text/event-stream responses with data: lines, automatic reconnect with Last-Event-ID, works with HTTP/2 and most proxies. WebSockets: full-duplex frames over an upgraded connection — needed for voice, interruptions or collaborative editing, but stateful and harder to scale and load-balance.
Quick prototype
Pipe the provider stream through.
In production, at scale
SSE with event IDs, heartbeat comments to keep proxies from timing out, disabled response buffering (X-Accel-Buffering: no), cancellation on disconnect, and sticky-free design (state in Redis or the job store) so any instance can resume a stream.
Vendors and tools
| sse-starlette / FastAPI StreamingResponse | SSE in Python |
|---|---|
| Socket.IO / ws | WebSockets |
| Vercel AI SDK | Streaming protocol for chat UIs |
On AWS: API Gateway WebSocket APIs; Lambda response streaming; ALB/ECS for long-lived SSE.
How it fails
- A proxy buffering the whole stream
- Idle connections dropped after 60 s without heartbeats
What the interviewer asks next
Why might SSE stop working behind your company proxy?
Buffering or compression middleware holds the response until it completes, or idle timeouts kill quiet connections. Disable buffering, send heartbeats, avoid gzip on the stream.