Skip to main content

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 WebSocket

Server-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 Events

WebSocket and Server-Sent Events compared

AspectWebSocketServer-Sent Events
DirectionTwo-way: client and server both sendOne-way: server to client only
ProtocolStarts as HTTP, then upgrades to ws:// or wss://Plain HTTP response of type text/event-stream
Data typesText and binary messagesUTF-8 text only
ReconnectionMust be handled in your own codeBuilt in, resuming with the Last-Event-ID header
InfrastructureProxies and load balancers must support the upgradeWorks with standard HTTP servers, proxies and HTTP/2
Browser APIThe WebSocket objectThe EventSource object
Best forChat, multiplayer games, collaborative editingNotifications, 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

WebSocketjavascript
// 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);
};
Server-Sent Eventsjavascript
// 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() request

Readers 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.

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings