Server-Sent Events vs WebSockets: Which One Do You Need?
Both keep a connection open so the server can push updates without being asked. SSE sends one way over plain HTTP. WebSockets carry messages both ways. Here's how to pick.
SSE vs WebSockets at a glance
|
Server-sent events |
WebSockets | |
|---|---|---|
|
Direction |
Server to client only |
Both ways, at any time |
|
Protocol |
Plain HTTP response with text/event-stream |
HTTP Upgrade handshake, then the WebSocket protocol (RFC 6455) |
|
Data |
UTF-8 text only |
Text and binary |
|
Reconnection |
Built in; resumes with Last-Event-ID |
You write it yourself |
|
Browser API |
EventSource |
WebSocket |
|
Connection limits |
6 per domain per browser on HTTP/1.1; multiplexed on HTTP/2 |
No per-domain cap like HTTP/1.1 |
|
Proxies and firewalls |
Normal HTTP, so it usually passes through |
Some proxies block or drop upgraded connections |
|
Best for |
Live feeds, notifications, AI token streaming |
Chat, multiplayer, collaborative editing |
What are server-sent events?
Server-sent events are part of the HTML standard. The browser makes an ordinary HTTP request, and instead of closing the response, the server keeps it open and writes events into it as they happen. Each event is a few lines of plain text: an optional event: name, an optional id:, and one or more data: lines, ending with a blank line.
On the client, the EventSource API does the work. It opens the request, parses the stream and fires a JavaScript event for each message. If the connection drops, the browser reconnects on its own and sends the last event ID it saw in a Last-Event-ID header, so the server can pick up where it left off. The server can also tell the client how long to wait before reconnecting with a retry: line.
The catch is direction. SSE only carries data from server to client. If the browser needs to send something back, it makes a separate HTTP request.
What are WebSockets?
A WebSocket starts as an HTTP request with an Upgrade: websocket header. If the server agrees, it replies with 101 Switching Protocols, and from then on the same TCP connection carries WebSocket frames instead of HTTP. Either side can send a message at any moment, as text or binary, with only a few bytes of framing overhead per message.
That makes WebSockets the default choice for anything conversational: chat, presence, typing indicators, multiplayer state. The trade-off is that you own more of the plumbing. There's no built-in reconnection or message replay, and load balancers have to support long-lived upgraded connections. For a full introduction, see What are WebSockets?.
What each looks like in code
An SSE client subscribes to a stream and listens for named events:
const events = new EventSource('/api/orders/stream');
events.addEventListener('order-update', (e) => {
const order = JSON.parse(e.data);
showToast(`Order ${order.id} is ${order.status}`);
});
events.onerror = () => {
// The browser retries automatically and sends Last-Event-ID
console.log('Stream interrupted, reconnecting');
};
The server side is a normal route that keeps writing to the response. In Node.js with Express:
app.get('/api/orders/stream', (req, res) => {
res.set({ 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache' });
res.flushHeaders();
const send = (order) =>
res.write(`event: order-update\nid: ${order.version}\ndata: ${JSON.stringify(order)}\n\n`);
orders.on('change', send);
req.on('close', () => orders.off('change', send));
});
A WebSocket client both sends and receives on the same connection:
const ws = new WebSocket('wss://example.com/chat');
ws.onopen = () => ws.send(JSON.stringify({ type: 'join', room: 'support' }));
ws.onmessage = (e) => renderMessage(JSON.parse(e.data));
ws.onclose = () => scheduleReconnect(); // reconnection is up to you
Key differences
Direction
This is the deciding question. If only the server talks, SSE is enough. If the client sends frequent messages too, a WebSocket avoids opening a fresh HTTP request for each one.
Reconnection and replay
SSE gives you automatic reconnection and resume-by-ID for free. With WebSockets you write a reconnect loop with backoff and decide how to catch up on missed messages, usually by fetching history from an API after reconnecting.
Browser connection limits
Over HTTP/1.1, browsers allow about six open connections per domain, shared across every tab. Open SSE in seven tabs and the seventh waits. HTTP/2 removes most of this by multiplexing many streams over one connection, so serve SSE over HTTP/2. WebSockets aren't counted against that HTTP/1.1 pool in the same way.
Authentication
EventSource can't set custom request headers, so you can't pass a bearer token the usual way. Use cookies or a short-lived token in the URL. A WebSocket can't set custom headers from the browser either; the common pattern is a token in the URL or a first message that authenticates the connection.
Infrastructure
SSE is plain HTTP, so it works through most proxies, CDNs and corporate firewalls, as long as they don't buffer the response. WebSockets need every hop to support the upgrade and keep idle connections alive, which sometimes means tuning timeouts on load balancers.
Other options: long polling and WebTransport
Long polling
Before SSE and WebSockets were widely supported, apps faked push with long polling: the client sends a request, the server holds it open until there's news, responds, and the client immediately asks again. It works everywhere, including behind strict proxies, which is why many real-time libraries still use it as a fallback. The cost is a new request and a full set of headers for every update, plus a short gap between response and the next request where events can bunch up.
WebTransport
WebTransport is a newer browser API that runs over HTTP/3 and QUIC. It offers bidirectional streams like WebSockets, plus unreliable datagrams for data where speed matters more than every packet arriving, such as game state or live media. Browser and server support is still uneven, so for most products in 2026 it's something to watch, not a default.
Scaling either one
Both SSE and WebSockets hold one open connection per user, so a single server eventually runs out of memory or file descriptors before it runs out of CPU. When you add servers, a message for a user may arrive on a different server from the one holding their connection. The usual fix is a shared pub/sub layer, such as Redis, that every server subscribes to, so any server can deliver to any connected user. Sticky sessions on the load balancer help with reconnects but don't remove the need for that shared layer.
When to use SSE or WebSockets
Use SSE for:
Notifications, activity feeds and live dashboards
Streaming AI responses token by token
Price tickers, scores and build or deploy logs
Use WebSockets for:
Chat, typing indicators and presence
Multiplayer games and shared cursors
Collaborative editing, where every keystroke goes both ways
If you're not sure, ask how often the client needs to send. Occasional sends (a like, a form submit) work fine as normal HTTP requests next to an SSE stream.
Rule of thumb: if the server does almost all the talking, start with SSE over HTTP/2. If users talk to each other in real time, use WebSockets.
Limits and pitfalls
Buffering proxies break SSE. If a proxy or compression layer buffers the response, events arrive in batches or not at all. Disable buffering for the stream route.
Idle connections get cut. Load balancers often close connections after 60 seconds of silence. Send a heartbeat (an SSE comment line, or a WebSocket ping) every 15 to 30 seconds.
Scaling is about open connections, not requests. Each user holds a connection for the whole session, so plan capacity by concurrent users, and share state across servers with a pub/sub layer.
No replay on WebSockets. Messages sent while a client was disconnected are gone unless you store them and let the client fetch what it missed.
SSE is text only. Binary data has to be encoded, which adds about a third to its size with Base64.
How CometChat handles real-time delivery
Chat is the classic two-way case: messages, typing indicators, read receipts and presence all flow in both directions, so building it on SSE alone means bolting on extra HTTP requests for everything the user sends.
WebSockets, managed for you
The CometChat SDKs deliver real-time events over a WebSocket connection that the SDK opens and maintains by default. If you need control, for example to disconnect when the app goes to the background, you can switch off automatic connection and call
connect()anddisconnect()yourself.
Frequently asked questions
Is SSE better than WebSockets?
Neither is better overall. SSE is simpler and enough when only the server sends updates. WebSockets are the right choice when both sides send messages often, as in chat or multiplayer apps.
Can server-sent events send data from client to server?
No. SSE only carries data from server to client. The client sends data with separate HTTP requests, such as fetch or POST calls.
Does SSE work with HTTP/2?
Yes, and it works better. HTTP/2 multiplexes many streams over one connection, which removes the six-connections-per-domain limit browsers apply over HTTP/1.1.
Why do AI chat apps use SSE?
Streaming a model's answer token by token is one-way traffic from server to client, which is exactly what SSE does. The user's prompt goes up as a normal HTTP request.
Do WebSockets reconnect automatically?
No. The browser WebSocket API doesn't reconnect on its own. You add a reconnect loop with backoff, or use a library or SDK that handles it.