# Choosing a Transport — WebSockets & Server-Sent Events

Source: https://www.skillbyai.com/en/realtime-web/rt-choosing

> Pick between polling, SSE, WebSockets and newer options for a given feature, and justify the choice.

## A decision guide

Start from the direction of data. If updates flow **only from server to client** (notifications, live scores, build logs, LLM token streams), **SSE** is usually the simplest choice: it is plain HTTP, works with existing auth cookies, reconnects automatically and resumes with `Last-Event-ID`. If the client also sends **frequent, low-latency messages** (chat with typing indicators, multiplayer games, collaborative editors, trading terminals), use a **WebSocket**, because each client message is a small frame on an open connection instead of a new HTTP request. If updates are rare or a minute of staleness is acceptable, **polling** is cheaper to build and operate. **WebTransport**, built on HTTP/3 and QUIC, offers multiple streams and unreliable datagrams, but browser and server support is still uneven, so treat it as specialised. Note that SSE is text only; binary data needs a WebSocket or a separate fetch.

## The decision as code

A tiny helper that encodes the guide. It is not a library, just a way to make the rules explicit.

```js
function chooseTransport({ clientSendsOften, needsBinary, maxStalenessSec }) {
  if (clientSendsOften || needsBinary) return 'WebSocket';
  if (maxStalenessSec >= 30) return 'polling';
  return 'Server-Sent Events';
}

console.log(chooseTransport({ clientSendsOften: true, needsBinary: false, maxStalenessSec: 0 }));
console.log(chooseTransport({ clientSendsOften: false, needsBinary: false, maxStalenessSec: 1 }));
console.log(chooseTransport({ clientSendsOften: false, needsBinary: false, maxStalenessSec: 60 }));
```

Output:

```
WebSocket
Server-Sent Events
polling
```

## Radio versus walkie-talkie

SSE is like FM radio: one station broadcasts, many listeners tune in, and if you lose signal you simply tune back. A WebSocket is a walkie-talkie: both sides transmit. You would not hand out walkie-talkies just so people can listen to the news.

## Interview trap: "WebSockets are always faster"

For server-to-client streams, SSE and WebSockets both deliver data over an already-open connection, so latency is similar. The real difference is two-way traffic, binary support and operational simplicity, not raw speed.

**Quiz:** An app streams an LLM's answer to the browser; the user only sends a prompt once per turn. Which transport fits best?

- [x] Server-Sent Events (or a streamed fetch response)
- [ ] A WebSocket with a custom binary protocol
- [ ] Short polling every 100 ms
- [ ] WebTransport datagrams

*Answer:* Server-Sent Events (or a streamed fetch response). Data flows one way, server to client, after a single request. A streamed HTTP response with SSE framing is simpler and is what most LLM APIs use.
