Mental model
WebSockets upgrade an HTTP socket into a full-duplex TCP connection. Server-Sent Events (SSE) maintain a standard persistent HTTP connection (text/event-stream) streaming text chunks from server to client.
Theory
- WebSockets (
ws://,wss://): Bi-directional, full-duplex binary framing over custom TCP protocol. Ideal for chat apps and multi-player collaboration. - Server-Sent Events (SSE): Unidirectional (server-to-client) UTF-8 text streaming over standard HTTP. Native browser auto-reconnect (
EventSource). Ideal for AI LLM token streaming and dashboard metrics.
Alternatives and trade-offs
- WebSockets: Bi-directional low latency; bypasses HTTP caching, requires proxy WebSocket protocol support (Nginx
Upgradeheaders). - SSE: Simple HTTP standard, works across existing HTTP load balancers and proxies, built-in browser retry; limited to text payloads and unidirectional push.
Failure modes and misconceptions
- Proxy Connection Drops: Reverse proxies (Nginx/Cloudflare) terminate silent SSE/WebSocket streams after
60sidle timeout. Always implement ping/pong or periodic heartbeat comments (: heartbeat\n\n). - Browser Connection Limits: Browsers limit HTTP/1.1 SSE connections to 6 per domain. Use HTTP/2 multiplexing to bypass connection caps.
Decision scenario
Use Server-Sent Events (SSE) for AI token streaming endpoints because SSE operates over standard HTTP, simplifies CORS/auth proxying, and provides native browser auto-reconnection.
Learning outcomes
- Compare WebSockets full-duplex framing with SSE HTTP streaming.
- Build resilient heartbeats and reconnection loops for long-lived streams.
- Avoid proxy dropouts and HTTP connection limits in browser streaming applications.
Trade-offs
WebSockets enable low-latency bi-directional messaging, while SSE provides simpler HTTP-compliant server-to-client streaming.