WebRTC: peer-to-peer media, signalling, NATs and media servers
How browsers exchange audio, video and data directly, how signalling, STUN, TURN and ICE get through firewalls, and when to add an SFU for group calls.
On the roadmap:Staff Engineer · step 4: Real-time at scale
Lesson 6 of 6 · 12 min read · quiz · exercise
What WebRTC is
WebRTC is a set of browser APIs and protocols for sending audio, video and arbitrary data directly between peers with low delay, for example for calls, screen sharing, games and file transfer. Media travels over UDP-based protocols and is encrypted (DTLS and SRTP) by default.
The problem it must solve
Two browsers behind home routers (NATs) usually have no public addresses and cannot just connect to each other. They first need to learn about each other, exchange what they can send and receive, and discover a network path that works.
Figure SDA-SQR-06-1 · Setting up a call · © forcsguy.com
The pieces
- Signalling is not part of WebRTC; you build it (a WebSocket service works well). It carries the session descriptions (SDP offer and answer) and ICE candidates. It also handles rooms, who is online and permissions.
- ICE (Interactive Connectivity Establishment) gathers candidate addresses and tests pairs to find a working path.
- STUN servers tell a peer its public address as seen from the internet. They are cheap and light.
- TURN servers relay the media when no direct path works, for example behind symmetric NATs or strict firewalls. Relaying costs real bandwidth, so use it as a fallback and monitor how often it is needed.
- Codecs and adaptation: the connection adapts bitrate to the available bandwidth, and can drop quality instead of stalling.
Figure SDA-SQR-06-2 · Direct path versus relay · © forcsguy.com
Beyond one-to-one: mesh, SFU and MCU
| Topology | How it works | Good for | Cost |
|---|---|---|---|
| Mesh | Every peer sends to every other peer | 2 to 4 people | Upload grows with participants; weak devices struggle |
| SFU (selective forwarding unit) | Each client sends one stream to a server, which forwards it to others without decoding | Most group calls and webinars | Server bandwidth; no transcoding, so low delay |
| MCU | Server decodes and mixes all streams into one | Legacy rooms, recording a mixed view | Heavy CPU, higher delay |
SFUs often use simulcast (the sender uploads a few quality layers) or scalable coding, so the server can send each receiver a layer that fits its bandwidth.
Figure SDA-SQR-06-3 · Mesh versus SFU for a group call · © forcsguy.com
Before: mesh: everyone sends to everyone
After: SFU: send once to a server
What changed
- Each person uploads one stream instead of many
- The server forwards streams without decoding them
- You now run and scale media servers
Design checklist
- Own signalling with authentication and room permissions; it is the control plane and a security boundary.
- Deploy STUN and TURN in several regions near users; measure the relay share and its bandwidth cost.
- Choose SFU for group calls; scale it by placing each room on one server and spreading rooms across servers, with regional placement for latency.
- Handle reconnection, device changes and poor networks (lower resolution, audio first).
- Plan for recording (SFU-side or a bot participant) and moderation separately; they are not free with peer-to-peer.
- For one-way live streaming to huge audiences, WebRTC is costly; HLS or DASH with a CDN scales better at higher latency (see the video streaming lessons).
Real-world reading
Related reading
Check your understanding
Question 1 of 3
0 correct so far
What is signalling in WebRTC?
Go deeper
- WebRTC: getting started overview by webrtc.org: the official introduction
- WebRTC connectivity by MDN Web Docs: ICE, STUN and TURN in plain language
- RFC 8445: Interactive Connectivity Establishment by IETF: the standard behind candidate gathering
Practise it: Signalling at scale
Everything below is already set up for this lesson: change the design, run the traffic, and see whether you meet the goal.
- 1The task
- 2Build and test
Signalling messages for 1,800 requests per second go through one WebSocket gateway. Media flows peer to peer, but setup still needs this path.
Goal: Under 1% errors, queue clears in 30 s.
The gateway carries every signalling message. Is one instance enough?
Try it yourself
A video meeting product wants rooms of up to 8 people at 1.5 Mbps per video stream. Compute the upload for one participant in a mesh versus with an SFU, and decide what to build.
Click what a strong design here would need:
More in Building blocks
© 2026 forcsguy.com. Original lesson text and diagrams. Books and sites linked above belong to their authors.