Skip to main content
Version: v10.7.1

WebSocket alternatives

Most apps that reach for a WebSocket only need data to flow one way, from the server to the browser. For that, Server-Sent Events or a streamed HTTP response is simpler and runs through every proxy your API already uses. Keep the WebSocket when the client sends a steady stream too. The other alternatives, WebTransport, WebRTC, MQTT and gRPC, solve narrower problems, and this guide says which ones.

What you'll learn
  1. How to pick an alternative by direction, frequency and who is on the other end.
  2. What SSE, polling, long polling and a streamed response each cost.
  3. Where WebTransport, WebRTC, MQTT and gRPC fit, and where they do not.
  4. What is replacing WebSockets, and why the honest answer is "nothing, for most apps".

The short answer​

AlternativeDirectionUse it forThe catch
Server-Sent EventsServer to clientNotifications, feeds, dashboardsUTF-8 text only, needs HTTP/2 for many tabs
Streamed HTTP responseServer to client, one requestOne AI completion, a long exportEnds with the request
PollingClient asks on a timerData that changes every few seconds or slowerMost requests come back empty
Long pollingClient asks, server answers when readyEnvironments that block everything elseOne full request per message
WebTransportBoth, over HTTP/3Many streams, or datagrams that may be droppedNewer, server support is thin
WebRTC data channelPeer to peerLow-latency data between two usersSignaling, NAT traversal, TURN servers
MQTTThrough a brokerDevice fleets and IoTA broker in the middle, WebSocket in the browser anyway
gRPC streamingBoth, between servicesTyped service-to-service streamsBrowsers get unary and server streaming only

Ask three questions in this order. Does the client send more than the odd request? If not, SSE or a streamed response. Does a late message still matter? If not, look at WebTransport datagrams or a WebRTC data channel. Is the other end a device or another service rather than a browser? Then MQTT or gRPC.


Server-Sent Events​

SSE is a long-running HTTP response with the text/event-stream content type. The browser's EventSource reads it, reconnects on its own and sends back the last event id so the server can replay what was missed (MDN: Using server-sent events). When the client has something to say, it makes a normal request.

This covers the most common reason people open a WebSocket: notifications, activity feeds, live prices, build logs and progress bars. It also keeps the traffic inside HTTP, so your auth middleware, logging and CDN rules still apply.

Two limits decide whether it fits. The stream is UTF-8 text, so binary data has to be encoded. And over HTTP/1.1 each open stream takes one of six connections per domain, shared by every tab, so serve it over HTTP/2. SSE vs WebSocket goes through the rest.


A streamed HTTP response​

When the stream belongs to one request, you need neither SSE nor a socket. An AI completion, a long CSV export or a report that renders row by row can be a plain fetch whose body arrives in chunks. The connection closes when the answer is done, and there is no reconnect logic to write.

In HyperFetch, useStream sends the request and grows text as each chunk arrives:

src/answer.tsx
import { useStream } from "@hyper-fetch/react";
import { askQuestion } from "./api";

function Answer({ question }: { question: string }) {
const { text, streaming, start, abort } = useStream(
askQuestion.setPayload({ question }),
);

return (
<div>
<p className="whitespace-pre-wrap">{text}</p>
<button type="button" onClick={streaming ? abort : start}>
{streaming ? "Stop" : "Ask"}
</button>
</div>
);
}

Polling and long polling​

Polling asks the same endpoint on a timer. It is the cheapest thing to build and to run when the data changes every few seconds or slower and the number of clients is small. It turns wasteful when updates must land within a second, or when thousands of clients poll and almost every answer is "nothing new".

In HyperFetch, polling is two options on useFetch:

Polling every 15 seconds
import { Time } from "@hyper-fetch/core";
import { useFetch } from "@hyper-fetch/react";

const { data } = useFetch(getBuildStatus, {
refresh: true,
refreshTime: Time.SEC * 15,
});

Long polling keeps the request open on the server until there is something to send, answers, and the client asks again straight away. Messages arrive almost as fast as over a socket, and it works through proxies that block everything else, which is why Socket.IO uses it as a fallback. The price is a full HTTP request, headers and all, for every message, plus a server that can hold many idle requests open.


WebTransport​

MDN describes WebTransport as "a modern update to WebSockets" (MDN: WebTransport API). It runs over HTTP/3, so one connection carries many independent streams, and a slow stream does not hold the others up. It also has datagrams: small messages that may arrive out of order or not at all, which is what you want for game state or cursor positions.

It is the closest thing to a successor. Today it is a choice for teams that control the server and can run HTTP/3 with WebTransport support, and whose users are on browsers that ship it. Browser support arrived years after WebSocket and is still uneven, so read the compatibility table before you plan around it.


WebRTC data channels​

A WebRTC data channel connects two browsers directly. It can be set to unordered and unreliable, so a lost packet is skipped instead of blocking the ones behind it. That makes it the right tool for latency between users: games, shared cursors, file transfer between peers.

It is not a drop-in replacement for a socket to your server. The peers still need a channel to exchange offers and ICE candidates first, and that channel is usually a WebSocket. WebRTC vs WebSocket shows the signaling code.


MQTT and gRPC​

These two replace a WebSocket only when the other end is not a browser.

MQTT is a publish and subscribe protocol for devices. Clients publish to topics on a broker, and the broker delivers to subscribers, with three delivery guarantees to choose from (mqtt.org). For sensors and device fleets it beats a hand-rolled socket protocol. In a browser, though, MQTT itself travels over a WebSocket.

gRPC gives services a typed contract and streaming in both directions over HTTP/2 (gRPC: Core concepts). Between your own services it is often the better choice. Browsers use gRPC-Web, which supports unary calls and server streaming only, through a proxy (gRPC-Web). So two-way traffic from a browser still goes over a WebSocket, and gRPC sits behind the server.


What is replacing WebSockets?​

For most apps, nothing is. What changed is that fewer features need one. The big LLM APIs stream their answers as Server-Sent Events, not over a socket, and HTTP/2 removed the six-connection limit that used to push people off SSE.

WebTransport is the real successor for two-way traffic, once your server, your hosting and your users' browsers all support it. Until then, chat, multiplayer state and collaborative editing stay on WebSockets, and they are still the only two-way transport that works in every browser and on every platform.


When to keep the WebSocket​

Keep it when
  • The client sends many small messages: chat with typing indicators, presence, collaborative edits.
  • You need binary frames in both directions.
  • The server is not yours and it speaks WebSocket: exchange feeds, managed realtime services, IoT gateways.
  • Your users are on browsers or platforms that do not support WebTransport yet.

Switching between WebSocket and SSE in HyperFetch​

In HyperFetch the transport is an adapter, so moving a receive-only feature from a WebSocket to SSE changes the socket definition and nothing else. Listeners, useListener and their types stay the same.

src/realtime.ts
import {
createSocketClient,
ServerSentEventsAdapter,
WebsocketAdapter,
} from "@hyper-fetch/sockets";

type Notification = { id: string; title: string };

export const socket = createSocketClient({
url: "wss://api.example.com/ws",
adapter: WebsocketAdapter(),
});
export const socket = createSocketClient({
url: "https://api.example.com/events",
adapter: ServerSentEventsAdapter(),
});

export const onNotification = socket.createListener<Notification>()({
topic: "notifications",
});

HyperFetch has no WebTransport, WebRTC, MQTT or gRPC adapter. The SocketAdapter class is public if you need to write one, as the adapter reference explains.


Congratulations!

You can now choose a WebSocket alternative, or decide to keep the socket.

  • You can pick SSE or a streamed response when data only flows from the server.
  • You can use polling for slow data and know when long polling is worth its cost.
  • You can place WebTransport, WebRTC, MQTT and gRPC by what is on the other end.
  • You can move a feature between WebsocketAdapter and ServerSentEventsAdapter without touching listeners.

Next steps​

SSE vs WebSocket
Direction, HTTP/2, reconnects, binary data and scaling.
HTTP vs WebSocket
Request and response, polling, webhooks and mixing both.