Skip to content

Lesson 4 of 9 · 28m

ICE, STUN, TURN, and Candidate Selection

Understand host, server-reflexive, relayed, and peer-reflexive candidates; configure STUN/TURN correctly; and read candidate pairs like a network engineer.

ICE is the part of WebRTC that turns two browsers behind hostile networks into something resembling connectivity. It is also the part most teams treat as magic until ten percent of calls never connect. The mental model is simple: gather every plausible address, exchange them, test pairs simultaneously, choose the best working pair, and keep testing because networks change.

Candidates are addresses with a cost model

A browser normally gathers several kinds: host is a local interface address, srflx is the public mapping discovered through STUN, relay is an allocation on a TURN server, and prflx is a peer-reflexive address learned during checks. Real candidate lines look like this:

a=candidate:1 1 UDP 2130706431 192.168.1.20 54321 typ host
a=candidate:2 1 UDP 1694498815 203.0.113.44 54321 typ srflx raddr 192.168.1.20 rport 54321
a=candidate:3 1 UDP 16777215 198.51.100.8 62000 typ relay raddr 203.0.113.44 rport 54321

Higher priority generally wins after connectivity checks. Host is preferred because it is cheap and direct; relay is last because relay bandwidth costs money and adds latency.

STUN discovers; TURN relays

STUN answers the question "what does the outside world see as my address?" It does not relay media. TURN allocates a relayed transport and forwards packets when direct paths fail.

A minimal coturn configuration is:

listening-port=3478
realm=rtc.internal
use-auth-secret
static-auth-secret=replace-with-high-entropy-secret
fingerprint
no-multicast-peers
denied-peer-ip=10.0.0.0-10.255.255.255
denied-peer-ip=192.168.0.0-192.168.255.255
total-quota=2000
log-file=stdout

Do not expose TURN to internal IP space unless you intentionally want it used as a proxy into your network. Treat relay as internet-facing infrastructure with quota, monitoring, authentication, and abuse controls.

Trickle ICE is the default operational pattern

Do not wait for gathering to finish before signaling. Send candidates as they appear:

pc.onicecandidate = event => {
  if (event.candidate) {
    sendSignal({type: "ice", candidate: event.candidate.toJSON()});
  } else {
    sendSignal({type: "ice-complete"});
  }
};

async function onRemoteIce(msg) {
  if (msg.candidate) await pc.addIceCandidate(msg.candidate);
}

addIceCandidate can fail if the remote description has not arrived yet. Queue candidates until setRemoteDescription completes. Dropping them produces intermittent one-way or failed calls that look network-related but are really ordering bugs.

Read candidate pair state, not vibes

Chrome exposes selected pair information through getStats():

for (const report of (await pc.getStats()).values()) {
  if (report.type === "candidate-pair" && report.state === "succeeded") {
    console.log({local: report.localCandidateId, remote: report.remoteCandidateId,
      rtt: report.currentRoundTripTime, writable: report.writable, nominated: report.nominated});
  }
}

Look up those IDs in the local and remote candidate reports. A successful relay-to-relay pair means connectivity exists but direct path discovery failed. A successful host pair inside different customer networks often means they are actually on the same LAN or VPN. Full-cone, restricted-cone, port-restricted, and symmetric NAT are older labels, but the operational reality remains: some mappings are endpoint-dependent. If either side changes its public mapping per destination, server-reflexive candidates become insufficient. That is why TURN exists. Expect some enterprise proxies and carrier-grade NAT environments to require relay.

Field notes

  • UDP is preferred for latency, but TCP and TLS-over-443 fallback matter in restrictive enterprise networks.
  • A STUN-only deployment is a direct-connect experiment, not a complete production design.
  • If one side never emits candidates, check permissions, browser support, network interfaces, and ICE gathering errors before blaming the remote network.
  • Candidate IP addresses can reveal local network topology. iceTransportPolicy: "relay" and mDNS hostnames reduce exposure in privacy-sensitive contexts.

Was this lesson helpful?

Sign in to rate