SSE vs WebSocket Performance Compared
SSE vs WebSocket from a Software Architect view: latency, overhead, LLM streaming, bidirectional realtime, and scaling to 100k connections.
You open ChatGPT, type a question, and words appear one by one as if someone is typing live. Where does that feeling of โspeedโ come from?
Most people guess โWebSocket must be the fastestโ. The surprising truth: most LLM APIs today (OpenAI, Anthropic, DeepSeek) stream tokens over SSE (Server-Sent Events) โ a far simpler technology than WebSocket.
So when does SSE win? When does WebSocket win? And if you want to scale to 10k, 100k, or 1 million connections, which one is the real bottleneck?
This post answers from the perspective of a Software Architect specializing in realtime systems and high-performance backends.
1. Explain Like Iโm 10 (ELI5)#
Imagine two ways to communicate:
- SSE is like a school loudspeaker: only the principal (server) speaks, the whole school (clients) listens. One-way, simple, everyone hears clearly. A student who wants to reply must raise a hand and walk to a separate room (a different HTTP request).
- WebSocket is like a two-way video call: both sides talk continuously over the same line. More flexible, but you must set up the call (handshake), hold the line, and handle drops yourself (manual reconnect).
[!NOTE] SSE = one-way (server โ client) over plain HTTP. WebSocket = two-way (client โ server) over one persistent TCP connection.
2. How they work#
SSE: a dangling HTTP connection#
The client sends a normal GET request, the server never answers fully โ it keeps the connection open and pours text down gradually:
Strengths:
- Passes through every proxy, CDN, and load balancer because it is just HTTP.
- Browsers auto-reconnect and send
Last-Event-IDto resume. - Easy to debug: open the Network tab and watch text flow.
Weaknesses:
- Server โ client only. Client โ server needs a separate HTTP endpoint.
- Text only (UTF-8); binary needs base64.
WebSocket: handshake once, talk forever#
The client upgrades from HTTP to WebSocket exactly once, then both sides exchange binary frames:
Strengths:
- Native bidirectional messaging on the same connection.
- Frame overhead of only 2โ6 bytes (vs ~600 bytes of HTTP headers per request).
- Supports both text and binary.
Weaknesses:
- You own reconnect, heartbeat, resume, and re-auth logic.
- Some proxies/CDNs need special config for the upgrade handshake.
- No HTTP/2 multiplexing benefit like SSE gets.
3. Quick comparison (TL;DR)#
| Criterion | SSE | WebSocket |
|---|---|---|
| Latency | ๐ข Very low | ๐ข Very low, usually a bit lower |
| Server โ client | ๐ข Excellent | ๐ข Excellent |
| Client โ server | ๐ก Needs another HTTP call | ๐ข Native |
| Protocol overhead | ๐ข Low | ๐ข Very low (2โ6 byte frames) |
| Backend CPU | ๐ข Usually simpler | ๐ก Slightly higher |
| Memory | ๐ข Good | ๐ข Good |
| 10k connections | ๐ข Can be fine | ๐ข Can be fine |
| 100k+ connections | ๐ก Depends on stack/proxy | ๐ข Usually a better fit |
| LLM streaming | ๐ข Great fit | ๐ข |
| Bidirectional realtime | โ | ๐ข Great fit |
[!TIP] Rule of thumb: server pushes down only โ SSE. Both sides talk continuously โ WebSocket.
4. Case 1: one-way streaming (LLM chat)#
The server sends:
token 1
token 2
token 3
token 4
...textWith SSE:
Server โโโโโโโโโโโโโโโโโโโโโโโ> Client
stream data continuouslytextVery efficient. A Node.js server takes a few lines:
import express from 'express';
const app = express();
app.get('/api/chat/stream', async (req, res) => {
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');
res.flushHeaders();
for await (const token of generateLLMResponse('Hello')) {
res.write(`data: ${JSON.stringify({ token })}\n\n`);
}
res.write('data: [DONE]\n\n');
res.end();
});
async function* generateLLMResponse(_prompt: string) {
for (const t of ['Hel', 'lo', ' there', '!']) {
await new Promise((r) => setTimeout(r, 50));
yield t;
}
}
app.listen(3000);typescriptThe browser client needs no library:
const es = new EventSource('/api/chat/stream');
es.onmessage = (e) => {
if (e.data === '[DONE]') return es.close();
appendToChat(JSON.parse(e.data).token);
};javascriptWebSocket can do it too:
WebSocket frame
WebSocket frame
WebSocket frame
...textAlso very fast โ but you never use its bidirectional advantage. Like buying a flagship phone just to listen to the radio.
5. Case 2: tiny, frequent, bidirectional messages#
Think games, cursor positions, collaborative editing:
Client โ position
Server โ position
Client โ position
Server โ position
...textSay 100โ1000 messages/second/client.
WebSocket is usually the better fit here, because the connection is designed for continuous two-way traffic with low frame overhead โ no new HTTP request per upstream message.
Each HTTP request carries ~600 bytes of headers. A WebSocket frame adds 2โ6 bytes. Multiply by 100 msg/s ร 10k clients and the bandwidth gap is very real.
// WebSocket: one connection, send and receive freely
const ws = new WebSocket('wss://example.com/agent');
ws.onmessage = (e) => renderAgentEvent(JSON.parse(e.data));
// Cancel mid-stream, approve a tool call, send position โ same wire
ws.send(JSON.stringify({ type: 'cancel' }));
ws.send(JSON.stringify({ type: 'approve-tool-call', id: 'tool-123' }));javascript6. The important caveat: donโt expect 10x#
Donโt expect:
WebSocket is 10x faster than SSE.
In real applications, latency is dominated by:
LLM generation
โ
database
โ
business logic
โ
network
โ
browser renderingtextnot by SSE vs WebSocket themselves.
If the LLM takes 500ms to produce the first token, WebSocket beating SSE by a few ms is irrelevant. Localhost benchmarks show ~18ms average WebSocket latency at 1000 connections โ slightly better โ but for pure server โ client broadcast to many passive clients, SSE is often leaner thanks to plain HTTP semantics.
[!WARNING] Optimizing the wrong layer is a classic disease: switching protocols without measuring LLM TTFT (time-to-first-token), DB queries, or render cost rarely improves UX.
7. For your AI project: what to choose?#
If the goal is AI chat / LLM streaming, I would choose:
POST /api/chat
โผ
Server
โผ
SSE /api/chat/stream
โโtoken
โโtoken
โโtoken
โโtoken
โโdonetextOver WebSocket. Reason: simpler, passes through any infrastructure, auto-reconnects, fast enough.
But if you later build:
AI Agent
โ
Browser
โ
Tool execution
โ
Terminal
โ
Agent eventstextwith many bidirectional realtime events (cancel generation, approve tool calls, mid-stream steering, multi-device sync), WebSocket starts making much more sense.
Decision checklist:
- Stream tokens down only? โ SSE.
- Need cancel/steer/approve from the client mid-stream? โ WebSocket (or SSE + extra HTTP POSTs, which gets messy as interactions grow).
- Need binary (realtime audio/video)? โ WebSocket (or WebRTC).
- Behind old CDNs/proxies? โ SSE is safer.
8. Scaling to 10k / 100k / 1M connections#
If your goal is 10k / 100k / 1M concurrent connections, the story changes a lot โ SSE vs WebSocket is only a small part. What matters more:
- Connection architecture: event loop (Node.js, Go, Elixir), sticky sessions, sharding.
- Proxy/load balancer: can Nginx, ALB, Cloudflare hold keep-alive + upgrade?
- Kernel/socket limits: file descriptors, TCP tuning, TIME_WAIT.
- Backpressure: where do you buffer when the server is faster than the client?
- Resume/reconnect strategy when mass drops happen.
In short:
SSE = very good performance + simpler for one-way streaming. WebSocket = better performance for high-frequency bidirectional realtime.
References#
- WebSocket vs SSE: Which One Should You Use? โ WebSocket.org โ
- WebSockets and AI: Why LLMs Are Moving Beyond SSE โ WebSocket.org โ
- AI Token Streaming: From SSE to Durable Sessions โ WebSocket.org โ
- WebSocket vs SSE vs HTTP: Which Should You Use? โ WebSocket.org โ
- How We Used SSE to Stream LLM Responses at Scale โ Dan A โ
- Realtime Benchmark: SSE vs WebSockets โ GitHub muratdemirci โ
- SSE vs WebSockets for LLM streaming: which to use in 2026 โ n4n.ai โ