Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 36 ยท Servers: STUN, TURN and SFU

STUN Servers and NAT: How Peers Find Each Other

Understand why home routers hide your address, how a STUN server reveals your public IP and port, what host and srflx candidates are, and how to configure iceServers.

Almost nobody's computer has a public IP address of its own. Behind a home or office router, your laptop uses a private address like 192.168.1.20, and the router translates it to one shared public address when you talk to the internet. This is NAT (Network Address Translation). It works wonderfully for loading web pages, but it creates a puzzle for WebRTC: two browsers, each behind its own NAT, need to send UDP packets directly to each other, and neither knows the address the outside world sees.

STUN servers solve the first half of that puzzle. They are tiny, cheap, and answer one question: "what address do you see me coming from?" This lesson explains NAT, what STUN does, and how to configure it. The second half, when direct paths fail, is the subject of TURN Servers and Relays.

Honest note about the sandbox: a preview on this site cannot reach the public internet through STUN in a way we can show reliably, so the live examples below use stand-ins (a candidate parser and a NAT simulator) and only read-only code for real STUN traffic.

What NAT does

When your laptop sends a UDP packet to a server, the router rewrites the source: 192.168.1.20:50000 becomes 203.0.113.7:61234, and remembers the pairing in a mapping table so replies can be sent back. Packets from outside that match no mapping are dropped. That is why peers cannot simply connect to each other: nothing is listening at an address the router has not already mapped.

Routers differ in how they build and protect mappings:

NAT behaviour Mapping rule Direct connection with STUN
Endpoint-independent mapping (cone) Same public port for every destination Usually works
Address- or port-dependent mapping (symmetric) New public port per destination Usually fails

A cone NAT keeps the same public ip:port however many destinations you contact, so the address STUN reports is also valid for your peer. A symmetric NAT creates a different public port for each destination, so the port STUN saw is useless to the peer. Symmetric NATs are common on mobile carriers and large corporate networks.

JS Stand-in: a tiny NAT simulator

Press Run (or Ctrl + Enter) to see the console output here.

No output.

With the cone NAT, the address STUN learned is the one the peer sees too, so hole punching can work. With the symmetric NAT the two differ, and you need a relay.

What a STUN server does

STUN (Session Traversal Utilities for NAT) is a simple request and response protocol over UDP. Your browser sends a Binding Request; the server answers with the source address it observed, called the XOR-MAPPED-ADDRESS. The server stores nothing, relays nothing, and uses almost no bandwidth, which is why public STUN servers are cheap to run, and why many are free.

sequenceDiagram
  participant L as Laptop 192.168.1.20
  participant N as NAT 203.0.113.7
  participant S as STUN server
  L->>N: Binding Request from 192.168.1.20:50000
  N->>S: Binding Request from 203.0.113.7:61234
  S-->>N: Binding Response: you are 203.0.113.7:61234
  N-->>L: Binding Response
  Note over L: server reflexive address learned

The server simply reflects back the public address it saw.

Candidates: host, srflx, relay

Each address your browser could be reached at is an ICE candidate. They come in kinds:

  • host: an address on your own network interfaces (for example 192.168.1.20).
  • srflx (server reflexive): the public address learned from STUN.
  • prflx (peer reflexive): a new address discovered while checking connectivity with the peer.
  • relay: an address on a TURN server that forwards traffic for you.

A candidate arrives as a text line. Reading one is a useful skill when you debug:

Text
candidate:842163049 1 udp 1677729535 203.0.113.7 61234 typ srflx raddr 192.168.1.20 rport 50000

The parts are: foundation, component (1 = RTP or data), transport, priority, IP, port, typ and its kind, then related address fields.

JS Parse and rank candidate lines

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Priorities favour direct paths: host first, then server reflexive, and relay last, because a relay costs bandwidth and adds delay.

Configuring STUN in code

You give the browser its servers through iceServers when you create the connection. Each entry has urls and, for TURN, credentials.

JavaScript
const pc = new RTCPeerConnection({
  iceServers: [
    { urls: 'stun:stun.example.org:3478' },
    { urls: ['stun:stun2.example.org:3478', 'stun:stun3.example.org:3478'] },
  ],
});

Use two or three STUN servers at most. More only slows gathering. Public STUN servers exist, but for a product you should run your own (or use the STUN that comes with your TURN server) so you control availability.

JS Read back the configuration

Press Run (or Ctrl + Enter) to see the console output here.

No output.

What STUN cannot do

STUN reports an address, but it cannot make a blocked path open. If both peers sit behind symmetric NATs, or a firewall drops all UDP, the connectivity checks fail and ICE needs a relay. In practice STUN alone succeeds for the majority of connections, while a TURN fallback rescues the remaining ones. Plan for both from day one.

Common mistakes

  • Listing many STUN servers. Two or three is enough.
  • Using a STUN server for private data. STUN is not secret: it only reveals the public address.
  • Assuming STUN relays media. It never does.
  • Forgetting the TURN fallback. A product that only works for "nice" networks fails for real users.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a STUN server tell your browser?

    A Binding Response contains the source address observed by the server, called the server reflexive address.
  2. 2. Which NAT type makes the address learned from STUN useless for a peer?

    A symmetric NAT assigns a new public port per destination, so the port seen by the STUN server is not the one the peer would see.
  3. 3. In 'typ srflx', what does srflx stand for?

    srflx means server reflexive: the address as reflected by a STUN server.
  4. 4. Which candidate type has the lowest priority and the highest cost?

    Relay candidates send everything through a TURN server, so ICE tries them last.

Summary

  • NAT hides private addresses; peers need to learn public ones.
  • STUN is a tiny reflector: it answers "what address do you see?" and relays nothing.
  • Candidate kinds: host, srflx, prflx, relay; ICE prefers direct ones.
  • Cone NATs work with STUN; symmetric NATs and strict firewalls need TURN.
  • Configure iceServers with two or three STUN URLs and a TURN fallback.

Next: TURN Servers and Relays