Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 36 ยท Peer Connections and ICE

ICE Candidates and NAT

Understand why peers need ICE: private addresses and NAT, candidate types (host, srflx, relay), candidate pairs, priorities, mDNS hostnames and connectivity checks.

If every computer had a unique public IP address, connecting two browsers would be trivial: send packets to the address. Reality is messier. Almost every device sits behind a router that performs NAT, a firewall may block inbound traffic, and mobile users switch between Wi-Fi and cellular. ICE (Interactive Connectivity Establishment) is the process WebRTC uses to discover every possible way to reach each peer and to find one that works.

What runs here. The sandboxed preview can only connect peers inside one page, so we see host candidates. Finding your public address (STUN) and relaying (TURN) need servers on the internet; those parts are explained with diagrams and read-only configuration, and the next chapters build on them.

The NAT problem in one picture

flowchart LR
  subgraph HomeA["Home A (private 192.168.1.x)"]
    A["Browser A<br/>192.168.1.20"] --> RA["Router A<br/>public 203.0.113.5"]
  end
  subgraph HomeB["Home B (private 10.0.0.x)"]
    B["Browser B<br/>10.0.0.8"] --> RB["Router B<br/>public 198.51.100.9"]
  end
  RA <--> I(("Internet"))
  RB <--> I

Browser A knows only its private address. Browser B cannot send to 192.168.1.20 from the internet. Someone must tell A its public address and make both routers open a path.

A NAT router rewrites outgoing packets so they appear to come from its public address and remembers the mapping so replies find their way back. It usually drops unsolicited incoming packets. The trick of ICE is to make both sides send packets to each other at the same time, so each router sees the other peer's packet as a reply to an outgoing one (a technique called hole punching).

Candidate types

A candidate is one possible transport address (IP, port, protocol) where a peer might be reached. ICE gathers several kinds:

Type Name Where it comes from Works when
host Host candidate A local network interface address Both peers are on the same network
srflx Server-reflexive The public address and port a STUN server saw your packet arrive from Most home NATs (hole punching)
relay Relayed An address on a TURN server that forwards your traffic Always, even through strict firewalls (costs bandwidth)
prflx Peer-reflexive Discovered during connectivity checks from the other peer's packets Learned on the fly

Each candidate line in SDP looks like this:

TEXT
candidate:784711854 1 udp 2113937151 192.168.1.20 55174 typ host generation 0 ufrag S+e2 network-cost 999
          ^foundation ^component ^protocol ^priority ^address ^port ^type

Gathering candidates yourself

Gathering starts when you call setLocalDescription. This example prints every candidate the browser finds in a readable form and counts types.

Gather and parse candidates

The number and kind of candidates depend on your machine and browser. You will see host candidates on at least one interface. If you add a STUN server to the configuration, you would normally also see srflx candidates; the preview cannot reach the internet, so we leave it out here.

mDNS hostnames

Browsers deliberately hide your private IP address from web pages: instead of 192.168.1.20 you see a random name such as 9dcf5e33-1c2c-4b20-bd15-f9a6b047ca89.local. The peer resolves that name through multicast DNS on the local network, which only works if both sides share the network. This is a privacy feature (it stops pages from learning your local IP through WebRTC), and it is why you cannot rely on parsing the IP out of host candidates. Once the user grants camera or microphone permission, some browsers expose real local IPs again.

Pairs, priorities and checks

After both sides have exchanged candidates, ICE forms candidate pairs: every local candidate with every remote candidate it could talk to. Each pair gets a priority, and ICE runs connectivity checks (STUN binding requests authenticated with the ice-ufrag and ice-pwd from the SDP) on the pairs in priority order. The first pair that succeeds in both directions is nominated and used for media.

Priorities are designed so cheaper and faster paths win: host over srflx over relay. The result is that a call on one LAN stays on the LAN, a call between homes uses a direct public path, and only the hardest cases use the relay.

You can read the winning pair from getStats():

Which candidate pair won?

Because both peers are on one machine, the winning pair is host to host and the round trip is nearly instant. Real calls between two homes would show srflx (or relay) and tens of milliseconds.

Why connections fail

Situation Result
Both peers behind friendly NATs srflx pair works with STUN
One peer behind a symmetric NAT (a new public port for every destination) Hole punching fails; needs TURN
Corporate firewall blocking UDP Needs TURN over TCP/TLS on port 443
No STUN or TURN configured Works on one LAN only
Candidates not exchanged (signalling bug) Stuck in checking, then failed

Rule of thumb for production: always configure STUN and TURN. STUN alone succeeds for most users; TURN catches the rest (roughly 10 to 20 percent in the real world, more in corporate networks). We set both up in chapter 6.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is a server-reflexive (srflx) candidate?

    STUN reports the public mapping created by your NAT; that mapping is the srflx candidate.
  2. 2. Why do host candidates show a random .local hostname?

    Using an mDNS name prevents pages from learning local IP addresses through WebRTC.
  3. 3. What does ICE do with candidate pairs?

    ICE checks pairs by priority and uses the first working, nominated pair.
  4. 4. When is a TURN relay typically needed?

    TURN is the fallback that works almost everywhere, at the cost of server bandwidth.

Summary

  • NAT hides private addresses; ICE finds a working path by gathering candidates, pairing them and testing the pairs.
  • Candidate types: host (local), srflx (public via STUN), relay (TURN) and prflx (learned during checks).
  • Browsers show mDNS hostnames instead of local IPs for privacy.
  • getStats() tells you which pair was selected; always configure STUN and TURN in production.

Next: Trickle ICE and ICE Restarts