Skip to main content
Version: v10.0.0

WebRTC vs WebSocket

The short answer to webrtc vs websocket: WebRTC connects two browsers directly, peer to peer, and is built for audio, video and low-latency data between them. A WebSocket connects a client to a server and carries messages through that server. They are not competitors so much as partners. Most WebRTC apps use a WebSocket to set up the call, then send the media over WebRTC.

This page explains that split, then compares WebSocket with the other options that come up in the same search: gRPC streams, MQTT and the rest of the WebSocket alternatives.

What you'll learn
  1. What WebRTC does that a WebSocket cannot, and the other way round.
  2. Why WebRTC signaling usually runs over a WebSocket, with a typed example.
  3. How gRPC vs WebSocket and WebSocket vs MQTT compare.
  4. Which WebSocket alternatives exist, and where HyperFetch fits among them.

What WebRTC is for​

MDN describes WebRTC as a way to "capture and optionally stream audio and/or video media, as well as to exchange arbitrary data between browsers without requiring an intermediary" (MDN: WebRTC API). The pieces you touch are RTCPeerConnection for the connection and media tracks, and RTCDataChannel for arbitrary data between the two peers.

"Without an intermediary" has fine print. Peers usually sit behind NAT and firewalls, so the browser uses ICE with STUN and TURN servers to find a route. TURN exists for the cases where two devices cannot reach each other directly (MDN: TURN). And before any of that, the peers need some way to exchange connection details. WebRTC leaves that part to you.

A data channel can also give up guarantees a WebSocket always keeps. Set ordered: false and maxRetransmits or maxPacketLifeTime, and messages may arrive out of order or not at all (MDN: createDataChannel). For game state or cursor positions, a late update is worthless anyway, so dropping it is the right call.

What a WebSocket is for​

A WebSocket is one long-lived, two-way connection between a client and a server (MDN: WebSockets API). Messages arrive in order and none are skipped while the connection is up. Everything goes through the server, which is exactly what you want for chat, notifications, live dashboards and shared state, because the server is the one that stores, checks and fans out the data.


WebRTC vs WebSocket side by side​

WebRTCWebSocket
Who talksPeer to peer, browser to browserClient to server
Built forAudio, video, low-latency dataMessages
Media tracksYesNo, you would ship encoded bytes yourself
DeliveryConfigurable per data channel, ordered or not, reliable or notOrdered and reliable
SetupSignaling, ICE, STUN and often TURN serversOne URL
Server sees the dataNo, unless it relays through TURN or a media serverAlways
Typical useCalls, screen sharing, peer file transferChat, feeds, notifications, collaboration

If every message has to be stored, checked or sent to many people, a server is in the path anyway, and a WebSocket is simpler. If two users need to see and hear each other with the least delay, WebRTC.


WebRTC signaling over WebSocket​

MDN is direct about the gap: "WebRTC doesn't specify a transport mechanism for the signaling information" (MDN: Signaling and video calling). Each side has to send the other an SDP offer or answer and a series of ICE candidates. The signaling server does not need to understand them, it only forwards them to the right user. A WebSocket fits well, because both the offer and the candidates have to arrive quickly and the server has to push them to the other peer.

Here is the caller's side with a HyperFetch socket as the signaling channel. The server forwards whatever arrives on signal/<peerId> to that peer.

src/signaling.ts
import { Socket } from "@hyper-fetch/sockets";

type Signal =
| { type: "offer" | "answer"; from: string; sdp: RTCSessionDescriptionInit }
| { type: "candidate"; from: string; candidate: RTCIceCandidateInit };

export const signaling = new Socket({ url: "wss://signal.example.com/ws" });

export const onSignal = signaling.createListener<Signal>()({ topic: "signal/:peerId" });
export const sendSignal = signaling.createEmitter<Signal>()({ topic: "signal/:peerId" });
src/call.ts
import { onSignal, sendSignal } from "./signaling";

const peer = new RTCPeerConnection({ iceServers: [{ urls: "stun:stun.example.com" }] });
const toBob = sendSignal.setParams({ peerId: "bob" });

// Send every ICE candidate to the other peer as it is found
peer.onicecandidate = ({ candidate }) => {
if (candidate) toBob.emit({ payload: { type: "candidate", from: "ana", candidate: candidate.toJSON() } });
};

// Read Bob's answer and candidates
onSignal.setParams({ peerId: "ana" }).listen(async ({ data }) => {
if (data.type === "answer") await peer.setRemoteDescription(data.sdp);
if (data.type === "candidate") await peer.addIceCandidate(data.candidate);
});

// Cursor positions: unordered, never retransmitted
const cursors = peer.createDataChannel("cursors", { ordered: false, maxRetransmits: 0 });

const offer = await peer.createOffer();
await peer.setLocalDescription(offer);
toBob.emit({ payload: { type: "offer", from: "ana", sdp: offer } });

The WebRTC calls are the browser's own API. HyperFetch only carries the signaling messages, with a typed payload, a topic per peer and reconnects if the signaling connection drops. Keep in mind that emit() does not queue: a candidate sent while the socket is down is lost, so restart the negotiation after a reconnect instead of assuming it went through. The emitting guide shows how to catch those errors.


gRPC vs WebSocket​

gRPC is an RPC framework. You describe services in Protocol Buffers, its default interface definition language, and generate clients and servers from that. It has four kinds of methods: unary, server streaming, client streaming and bidirectional streaming (gRPC: Core concepts), and it runs over HTTP/2 (gRPC over HTTP2).

Between your own services, gRPC is often the better tool: a typed contract, generated code in many languages, and streaming in both directions.

In the browser it is a different story. Browsers use gRPC-Web, which "currently supports 2 RPC modes": unary and server-side streaming. Client-side and bidirectional streaming are not supported, and clients connect through a proxy, Envoy by default (gRPC-Web). So for two-way realtime traffic from a browser, a WebSocket is still the usual choice, and many teams run gRPC behind the server and a WebSocket in front of it.


WebSocket vs MQTT​

MQTT is "an OASIS standard messaging protocol for the Internet of Things" and "an extremely lightweight publish/subscribe messaging transport" (mqtt.org). Clients do not talk to each other or to your app server. They publish to topics on a broker, and the broker delivers to subscribers. It has three quality of service levels: at most once, at least once and exactly once.

The two are not on the same layer. MQTT is a messaging protocol, and WebSocket is one of the transports it can use. The MQTT 5 specification has a section on it: packets go in binary WebSocket frames, and the client must offer the mqtt subprotocol (MQTT 5.0, section 6). That is how a browser dashboard reads the same topics as the sensors in the field.

Choose MQTT when you have many devices, unreliable networks and a broker in the middle anyway. Choose a plain WebSocket when the client talks to your own app server and the server decides what each user sees. If a browser needs MQTT, use an MQTT client library over WebSocket. HyperFetch does not speak MQTT.


WebSocket alternatives​

OptionDirectionGood forWatch out for
Server-Sent EventsServer to clientFeeds, notifications, AI token streamsText only, and HTTP/2 matters
PollingClient asksData that changes every few seconds or less oftenRequests that come back empty
WebTransportBothMultiple streams and unreliable datagrams over HTTP/3Newer, secure contexts only
WebRTC data channelPeer to peerLow-latency data between usersSignaling, NAT traversal
gRPC streamingBoth, between servicesTyped service-to-service streamsBrowsers only get unary and server streaming
MQTTThrough a brokerIoT and device fleetsNeeds a broker, and WebSocket in the browser

MDN calls WebTransport "a modern update to WebSockets" over HTTP/3, with multiple streams and UDP-like datagrams. Its compatibility box marks it Baseline 2026, newly available since March 2026, so older browsers will not have it (MDN: WebTransport API).


Where HyperFetch fits​

HyperFetch sockets cover the client-server half of this page. The @hyper-fetch/sockets package ships a WebsocketAdapter and a ServerSentEventsAdapter, with typed listeners and emitters, reconnects with jittered backoff between reconnectTime and maxReconnectTime, an optional heartbeat, and React hooks.

It has no WebRTC, gRPC, MQTT or WebTransport adapter. For a call, it is the signaling channel and the realtime layer around the call (presence, chat, who is in the room), while the media goes through WebRTC.

When HyperFetch is the right layer
  • Signaling for a WebRTC call, with typed offer, answer and candidate messages.
  • Chat, notifications and presence next to a call.
  • Client-server realtime for a browser, React or React Native app.
  • Receive-only feeds over SSE, with the same listener API as WebSocket.

The Node.js WebSocket client page shows the same socket running in a Node.js worker, and errors and reconnection explains what happens when the connection drops.


Which one fits you​

  • Video or voice calls, screen sharing, peer file transfer: WebRTC, with a WebSocket for signaling.
  • Chat, notifications, collaboration, live data from your server: WebSocket, or SSE if the client only receives.
  • Service-to-service streaming with a typed contract: gRPC.
  • Sensors and devices publishing through a broker: MQTT, over WebSocket when a browser joins in.
  • Many independent streams or unreliable datagrams in new browsers: look at WebTransport.
Summary
  • WebRTC is peer to peer and built for media and low-latency data. WebSocket is client to server and built for messages.
  • WebRTC does not define signaling, and a WebSocket is the usual way to exchange offers, answers and ICE candidates.
  • gRPC suits service-to-service streams, but browsers only get unary and server streaming through gRPC-Web.
  • MQTT is broker-based pub/sub and can run over WebSocket.
  • HyperFetch is a WebSocket and SSE client. It carries signaling and client-server realtime, not media.