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

TopologyHow it worksGood forCost
MeshEvery peer sends to every other peer2 to 4 peopleUpload grows with participants; weak devices struggle
SFU (selective forwarding unit)Each client sends one stream to a server, which forwards it to others without decodingMost group calls and webinarsServer bandwidth; no transcoding, so low delay
MCUServer decodes and mixes all streams into oneLegacy rooms, recording a mixed viewHeavy 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

Check your understanding

Question 1 of 3

0 correct so far

What is signalling in WebRTC?

Go deeper

© 2026 forcsguy.com. Original lesson text and diagrams. Books and sites linked above belong to their authors.