Skip to content
Beytullah Toprak

Blog

Real-Time APIs: WebSocket vs SSE, and When to Use Each

WebSocket gives you a two-way connection, SSE a one-way stream. How they differ, where each falls short, what AI APIs use and how to choose.

Reading time
6 min read
WebSocket vs SSE illustration with a chat bubble and a live chart icon facing each other
On this page
  1. What Is a WebSocket?
  2. What Are Server-Sent Events (SSE)?
  3. How WebSocket and SSE Differ
  4. Limits of SSE
  5. Limits of WebSocket
  6. What Do AI Applications Use?
  7. Example: A Live Order Tracking Screen
  8. Which Should You Choose?
  9. Frequently Asked Questions
  10. Is SSE outdated?
  11. Does WebSocket affect SEO?
  12. How do I secure a WebSocket connection?

A WebSocket opens a two-way connection between the browser and the server that stays open. SSE (Server-Sent Events) is a one-way stream of events from the server to the browser that runs over plain HTTP. The short answer to WebSocket vs SSE: if the user also sends data constantly (chat, multiplayer games, collaborative editing), pick WebSocket. If data mostly comes from the server (notifications, live scores, streaming an AI response), SSE is usually simpler and enough.

What Is a WebSocket?

WebSocket is a protocol for two-way messaging over a single TCP connection. RFC 6455, the WebSocket protocol standard (opens in a new tab) from December 2011, describes it as an opening handshake followed by message framing over TCP, designed so browser apps that need two-way communication don't have to open multiple HTTP connections.

MDN's WebSocket API page (opens in a new tab) notes that with this API you can send messages to a server and receive replies without polling it. Once the connection is open, either side can send a message at any time. That flexibility suits chat apps, games and live collaborative editing.

What Are Server-Sent Events (SSE)?

With SSE, the server sends a series of events to the browser over an open HTTP response. MDN's guide to server-sent events (opens in a new tab) states plainly that this is a one-way connection: the client can't send events to the server over the same channel. The server responds with the text/event-stream type, and each event is a block of text ending with two newlines:

event: price
id: 1042
data: {"symbol":"BTC","price":97450}

event: price
id: 1043
data: {"symbol":"BTC","price":97462}

In the browser, an EventSource object listens to the stream. According to the same guide, if the connection drops, the browser waits a while and tries to reconnect. The server can set that delay with the retry field. The id field sets the ID of the last event received. If your server reads that ID and sends any missed events, the stream picks up where it left off after a reconnect.

How WebSocket and SSE Differ

WebSocket SSE
Direction Two-way Server to client only
Protocol Separate protocol (ws://, wss://) Plain HTTP
Data Text and binary Text only (UTF-8)
Reconnect after a drop You write it The browser retries on its own
Resume where you left off You write it Browser keeps the last id, the server handles the rest
Infrastructure May need proxy and load balancer settings Works with existing HTTP infrastructure
Typical use Chat, games, collaborative editing Notifications, live feeds, AI responses

The first row decides most cases. If messages from client to server are occasional, sending them as regular HTTP requests and receiving server updates over SSE is often less work than setting up WebSockets.

Limits of SSE

SSE has one well-known weak spot: connection count. MDN's guide says that without HTTP/2, browsers allow at most 6 open connections per domain, shared across all tabs, and that Chrome and Firefox have marked this as "won't fix". Over HTTP/2, the number of simultaneous streams is negotiated between server and client and defaults to 100. So if you use SSE, make sure your server supports HTTP/2. Otherwise pages can start waiting for connections once a user opens a few tabs.

The second issue is buffering in the middle. Some proxy and server settings collect the response and send it in chunks, which breaks the stream. That's why MDN's sample code also sends headers that turn off caching and buffering. Before going live, check that events still arrive instantly behind your reverse proxy.

Limits of WebSocket

WebSocket's flexibility has a cost. According to MDN, the WebSocket interface doesn't support backpressure: if messages arrive faster than the app can handle them, the browser either buffers them until memory fills up or pegs the CPU and stops responding. MDN recommends the WebTransport API when backpressure matters. The same page notes that pages with an open WebSocket connection may not be added to the browser's back/forward cache.

There's more work on the server too. Each connection stays open for a long time, so once you scale to several servers you need a pub/sub layer such as Redis to deliver one user's message to connections held by other servers. Reconnecting dropped connections, refreshing authentication and catching up on missed messages are also left to your code.

What Do AI Applications Use?

Major language model providers such as OpenAI and Anthropic stream responses over SSE. OpenAI's guide to streaming responses (opens in a new tab) explains that streaming runs over HTTP using server-sent events by default, and separately mentions a WebSocket mode for persistent connections with incremental inputs. Anthropic's streaming documentation (opens in a new tab) likewise says Messages API responses, including text, tool use and thinking deltas, are delivered as server-sent events.

That gives you a good starting point for your own chat interface. Send the user's message to your server with a regular POST request, and have your server pass the model's stream on to the browser as SSE. Because the request is a POST, browsers usually read the stream with fetch rather than EventSource. I explain why model calls belong on the server in the AI API integration guide.

Example: A Live Order Tracking Screen

Picture a restaurant's kitchen display. New orders and status changes flow from the server to the screen. The only thing going back is a cook pressing the "ready" button. SSE is enough here: the screen listens to the order stream over SSE, and "ready" goes out as a regular POST request. If the connection drops, the browser reconnects. If the server is written to use the last event ID, it also resends any missed orders.

If the same restaurant adds a live support chat with customers, things change. Messages go back and forth often, and small, instant signals like a "typing" indicator travel too. WebSocket is the more natural choice for that.

Which Should You Choose?

These questions settle most cases:

  1. Does the client also send data often and with low latency? Then WebSocket.
  2. Will you carry binary data, such as audio or video chunks? Then WebSocket or WebTransport.
  3. Does data mostly come from the server, with the client sending the odd request? Then SSE plus regular HTTP.
  4. Are updates rare, say once a minute? You may need neither. Polling at an interval is the simplest option.

Notifying events between two systems, server to server, is a different topic. Webhooks or queues are the usual tools there, and I cover them in the post on event-driven architecture. For the broader API design picture, see REST vs GraphQL.

Frequently Asked Questions

Is SSE outdated?

No. Browsers have supported SSE for years, and OpenAI's and Anthropic's APIs use it for streaming responses today. Its simplicity is the main reason people overlook it.

Does WebSocket affect SEO?

Not directly. Content that search engines need to see should be in the HTML when the page first loads. Live updates should stay a layer on top of that.

How do I secure a WebSocket connection?

Always use encrypted wss://, authenticate the user when the connection opens and validate every incoming message as you would a normal HTTP request. RFC 6455 says the protocol relies on the origin-based security model browsers use, which is why checking the Origin header on your server matters.

If you're planning a web application with real-time features, see the web design and application development service or tell me about your project on the contact page.

Share