Library / Documentation
MDN: WebRTC connectivity
ICE, STUN, TURN and how peers find a path through NATs.
By MDN contributors · MDN Web Docs · Added 2026-10-12
All content belongs to MDN contributors / MDN Web Docs. We only link to it and give credit. Report a problem
Learn this doc
About 30 min left. Your work is saved as you go.
What you want from these docs
MDN Web Docs documents a tool you may use. Decide what you want to do with it, then read only what answers that.
Ideas you will meet
- WebSockets
- Persistent bidirectional connections for real-time features.
What we already know about this
Our own lessons on these ideas. Read them here before you start, so it makes sense.
Core Concepts: How Things Actually Work · 7 minPolling, long polling, SSE and WebSocketsFour ways to get live updates to a browser, and how to choose between them.Read here
The problem
HTTP is request and response: the client asks. How does the server tell a browser something now?
The four options
Technique How Good Weak Short polling Client asks every N seconds Trivial, works everywhere Wasteful, delayed by N Long polling Client asks; the server holds the request until there is news (or a timeout), then the client asks again Near real time, plain HTTP A held request per client, awkward at scale Server-sent events (SSE) One long HTTP response streams events Simple, auto-reconnect, text over HTTP One-way only WebSockets HTTP upgrades to a two-way connection Low latency both ways Stateful connections, more infrastructure 100%Figure SDA-CORE-08-1 · The four options · © forcsguy.com
Before: polling: keep asking
100%After: WebSocket: server pushes
100%What changed
- No wasted requests when nothing has changed
- Updates arrive the moment they happen
- The server must hold many open connections
Choosing
- Updates are rare or delays are fine: polling.
- The server pushes, the client mostly listens (feeds, scores, notifications, progress): SSE.
- Both sides send often (chat, games, collaborative editing): WebSockets.
Running persistent connections
Connections are long-lived, so capacity is about how many connections a server can hold, not requests per second.
- Put connection handling in servers that are separate from your stateless API.
- Use heartbeats to detect dead connections, and reconnect with backoff and jitter.
- Track which server holds which user so messages can be routed (a presence or session store), and use a pub/sub system between servers.
- When a client reconnects, let it ask for what it missed (events after id X), because connections drop.
- Authenticate on connect and again when tokens expire.
More on this site
Related reading
- RFC 6455: The WebSocket ProtocolIETFDocumentation
- MDN: WebSockets APIMDN Contributors · MozillaDocumentation
- Uber's Real-Time Push PlatformUber Engineering · UberCase Studies
- How Discord Scaled Elixir to 5,000,000 Concurrent UsersDiscordCase Studies
Show 6 moreShow fewer
- The WhatsApp Architecture Facebook Bought for $19BHigh ScalabilityCase Studies
- Erlang at Facebook (slides)Eugene Letuchy · FacebookCase Studies
- Slack EngineeringSlack engineers · SlackBlogs
- Real-time MessagingSlack Engineering · SlackCase Studies
- How Uber Scales Their Real-time Market Platform - High Scalability -High Scalability engineers · High ScalabilityCase Studies
- Real-Time Predictions at DoorDashDoorDash engineers · DoorDashCase Studies
Storage, Queues and Real-time · 12 minWebRTC: peer-to-peer media, signalling, NATs and media serversHow 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.Read here
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.
100%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.
100%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.
100%Figure SDA-SQR-06-3 · Mesh versus SFU for a group call · © forcsguy.com
Before: mesh: everyone sends to everyone
100%After: SFU: send once to a server
100%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
Go deeper on this
A path through videos, articles, other case studies and code on the same ideas as MDN: WebRTC connectivity.
Warm upGet the vocabulary first.
Books · Free from the author
High Performance Browser Networking
How TCP, TLS, HTTP/2, WebSocket and mobile networks affect web performance; free to read.
O'ReillyIntermediate
Documentation
Druid - Real-time Analytics Database
Apache Druid, a real-time analytics database: the project site and its documentation.
ApacheIntermediate
WatchHow others solve the same problem, on video.
Videos belong to their creators and play from YouTube. More on YouTube (opens in a new tab)
ReadEngineers explain it in their own words.
Blogs
Slack Engineering
How Slack builds messaging, search, job queues and infrastructure.
SlackIntermediate
Papers
RFC 8445: Interactive Connectivity Establishment (ICE)
The standard that defines how peers gather and test candidate network paths.
IETFAdvanced
See it in another companyThe same ideas, a different system.
Case Studies · Read here
Brooklin: Distributed Service for Near Real-Time Data Streaming at LinkedIn
Brooklin—a distributed service for streaming data in near real-time and at scale—has been running in production at LinkedIn since 2016, powering thousands of data streams and over 2 trillion messages per day. Today, w...
LinkedInIntermediate
Case Studies · Read here
Concourse: Generating Personalized Content Notifications in Near-Real-Time at LinkedIn
Social media are computer-mediated platforms that facilitate creation and sharing of information, ideas, career interests, and other forms of expression via virtual communities and networks. In the last few years, not...
LinkedInIntermediate
Keep learning
- Brooklin: Distributed Service for Near Real-Time Data Streaming at LinkedInCase Studies · Intermediate · LinkedIn
- Concourse: Generating Personalized Content Notifications in Near-Real-Time at LinkedInCase Studies · Intermediate · LinkedIn
- Druid - Real-time Analytics DatabaseDocumentation · Intermediate · Apache
Loading community…