Side by side
WebSocketvsServer-Sent Events
What is the difference between WebSocket and Server-Sent Events?
Updated 2 min read7 differences
In short
WebSocket opens a two-way channel where client and server both send messages anytime, while Server-Sent Events (SSE) let only the server push updates.
WebSocket
WebSocket is a protocol that keeps a single connection open between a browser and a server so both sides can send each other messages instantly at any time.
Read the page on WebSocketServer-Sent Events
Server-Sent Events is a web standard that lets a server push a continuous stream of text updates to the browser over one long-lived HTTP connection.
Read the page on Server-Sent EventsWebSocket and Server-Sent Events compared
| Aspect | WebSocket | Server-Sent Events |
|---|---|---|
| Direction | Two-way: client and server both send | One-way: server to client only |
| Protocol | Starts as HTTP, then upgrades to ws:// or wss:// | Plain HTTP response of type text/event-stream |
| Data types | Text and binary messages | UTF-8 text only |
| Reconnection | Must be handled in your own code | Built in, resuming with the Last-Event-ID header |
| Infrastructure | Proxies and load balancers must support the upgrade | Works with standard HTTP servers, proxies and HTTP/2 |
| Browser API | The WebSocket object | The EventSource object |
| Best for | Chat, multiplayer games, collaborative editing | Notifications, live feeds, dashboards, streamed AI output |
The difference, explained
WebSocket is a protocol that upgrades an HTTP connection into a persistent, full-duplex channel, so the browser and the server can each send text or binary messages whenever they like. Server-Sent Events (SSE) is a simpler browser standard: the client opens an ordinary HTTP request with the EventSource API, and the server keeps the response open and writes events to it over time.
The essential difference is direction. WebSocket is bidirectional and switches to its own message framing after the handshake, which suits chat, multiplayer games and collaborative editing. SSE flows only from server to client and stays plain HTTP, so it works with existing proxies, authentication and HTTP/2, and the browser reconnects automatically if the stream drops.
They cover overlapping needs, and many apps use SSE for notifications or live feeds plus ordinary fetch requests for the few messages the client sends. Streaming answers from AI models into a chat interface is a well-known modern use of SSE-style streams. WebSocket becomes the better fit when the client also sends frequent, low-latency messages.
A common misconception is that SSE is outdated or limited to a handful of connections. The limit of six connections per domain applies only over HTTP/1.1; over HTTP/2 or HTTP/3, many streams share one connection. The real limits are that SSE carries only UTF-8 text and only flows from server to client.
Which one should you use?
Choose WebSocket when…
- The client sends frequent messages, not just the server.
- You need binary data or very low latency in both directions.
- You are building chat, multiplayer games or real-time collaboration.
Choose Server-Sent Events when…
- Only the server needs to push updates.
- You want automatic reconnection without extra code.
- You prefer plain HTTP that works with existing proxies, auth and HTTP/2.
- You stream text such as notifications, logs or AI responses.
Receiving live updates in the browser
// WebSocket: both sides can send at any time
const socket = new WebSocket("wss://example.com/chat");
socket.onopen = () => socket.send("Hello, server!");
socket.onmessage = (event) => {
console.log("Server says:", event.data);
};// SSE: the server pushes, the browser only listens
const events = new EventSource("/api/notifications");
events.onmessage = (event) => {
console.log("New update:", event.data);
};
// To send data, the client makes a normal fetch() requestReaders ask
Is SSE better than WebSocket?
Neither is better in general. SSE is simpler when only the server pushes data, while WebSocket is the right tool when both sides need to send messages with low latency.
Does SSE work with HTTP/2?
Yes, and it works better there, because many SSE streams can share one HTTP/2 connection instead of each needing its own.
Is WebSocket the same as HTTP?
No. A WebSocket connection starts with an HTTP handshake, but after the upgrade it uses its own protocol of message frames rather than requests and responses.