Definition

What Are STUN and TURN Servers? How WebRTC Gets Through NAT

Most devices sit behind a router that hides their real address. STUN and TURN are how two of them find each other for a call anyway.

STUN vs TURN

STUN

TURN

What it does

Tells a client its public IP and port

Relays media between peers

Does media pass through it?

No

Yes, every packet

Bandwidth cost

Tiny; a few small requests per call

The full call bitrate, in and out

When it is used

On almost every connection attempt

Only when a direct path fails

Authentication

Usually none for basic lookups

Required; username and credential

Latency

None added to the call

Adds a hop, so placement matters

Standard

RFC 8489

RFC 8656

What are STUN and TURN servers?

A WebRTC call sends audio and video between two devices, ideally directly. The problem is that most devices don't have a public address. They sit behind a home router, an office firewall or a mobile carrier's NAT (network address translation), which maps many private addresses to a few public ones and blocks traffic nobody asked for.

STUN (Session Traversal Utilities for NAT) solves the first half. A client sends a small request to a STUN server on the public internet, and the server replies with the IP address and port it saw the request come from. That is the client's public-facing address, which it can share with the other peer.

TURN (Traversal Using Relays around NAT) is the fallback. When the two peers can't reach each other directly, both connect to a TURN server, and it relays every packet between them.

Neither one sets up the call. A signaling server carries the messages that let the peers exchange addresses in the first place.

How ICE uses STUN and TURN

WebRTC uses ICE (Interactive Connectivity Establishment) to find a working path. It runs in four steps:

  1. Gather candidates. Each peer collects possible addresses: host candidates (its local IPs), server-reflexive candidates (its public address, learned from STUN) and relay candidates (an address on a TURN server).

  2. Exchange candidates. The peers send their candidates to each other through the signaling channel, often one at a time as they are found (trickle ICE).

  3. Check connectivity. ICE pairs local and remote candidates and sends STUN checks over each pair to see which ones actually get through.

  4. Pick a pair. ICE prefers direct paths and uses a relay only when nothing else works. If the network changes mid-call, an ICE restart repeats the process.

Direct connections fail most often when both sides are behind restrictive NATs (symmetric NATs give a different public port for each destination) or when a firewall blocks UDP entirely. TURN can also run over TCP or TLS on port 443, which gets through most corporate firewalls.

STUN and TURN config in JavaScript

You pass STUN and TURN servers to the browser with the iceServers option. TURN needs credentials, so fetch short-lived ones from your backend instead of hard-coding them.

const turn = await fetch('/api/turn-credentials').then(r => r.json());

const pc = new RTCPeerConnection({
  iceServers: [
    { urls: 'stun:stun.example.com:3478' },
    {
      urls: [
        'turn:turn.example.com:3478?transport=udp',
        'turns:turn.example.com:443?transport=tcp'   // gets through strict firewalls
      ],
      username: turn.username,     // expires after a few hours
      credential: turn.credential
    }
  ]
  // iceTransportPolicy: 'relay'   // force TURN, e.g. to hide users' IPs
});

pc.onicecandidate = ({ candidate }) => {
  if (candidate) signaling.send({ type: 'candidate', candidate });
};
pc.oniceconnectionstatechange = () => console.log('ICE:', pc.iceConnectionState);

In chrome://webrtc-internals you can see which candidate pair a call picked, and whether it is relay.

Why TURN relays cost bandwidth

A STUN server answers a few tiny requests per call. A TURN server carries the call itself. Every audio and video packet goes from the sender to the relay and from the relay to the receiver, so the server handles each stream twice: once in, once out.

A one-to-one video call at 1.5 Mbps in each direction puts about 3 Mbps into the relay and 3 Mbps out, for the whole call. Multiply by concurrent calls and TURN quickly becomes the largest line on a self-hosted calling bill. Relayed calls also add latency, because media takes a detour through the server, so TURN servers need to sit close to users.

Only a minority of connections need TURN, but you can't predict which. Users on corporate networks, hotel Wi-Fi and some mobile carriers fail without it, so production apps always configure it.

Rule of thumb: STUN is cheap, so always include it. TURN is expensive, so always include it too, and track how often calls use it.

Where STUN and TURN are used

  • One-to-one calls: in-app voice and video between two users, such as a buyer and seller or a patient and doctor. STUN finds the direct path, TURN rescues the rest.

  • Group calls through an SFU: clients still use ICE to reach the media server, and TURN over TLS helps users behind strict firewalls.

  • Data channels: peer-to-peer file transfer or game state over RTCDataChannel uses the same ICE process.

  • Privacy: forcing relay-only connections stops peers from learning each other's IP addresses.

Limits and pitfalls

  • STUN alone isn't enough. A demo on one network works fine and then fails for real users behind restrictive NATs. Always configure TURN for production.

  • Static TURN credentials get abused. Anyone who reads them from your JavaScript can relay traffic on your bill. Issue short-lived credentials from your server.

  • Public STUN servers have no guarantees. Fine for testing; for production, run your own or use your provider's.

  • UDP-only TURN misses firewalls. Offer TURN over TCP or TLS on port 443 as well.

  • One region adds latency. Relayed media travels to the TURN server and back, so place relays near your users.

  • Watch for silent relay costs. Track how many calls end up relayed. A jump usually points to a network or configuration change.

Connectivity in CometChat Calls

If you build calling on raw WebRTC, STUN, TURN, credentials and relay placement are yours to run. A calling SDK can take most of that on.

WebRTC calling without hand-written ICE code

CometChat Calls is built on WebRTC, and its docs say the SDK handles establishing peer connections and managing ICE candidates for you. Group calls run through CometChat's SFU media servers, and the SDK reconnects automatically after network interruptions. You add 1:1 and group voice and video with SDKs for JavaScript, iOS, Android, React Native and Flutter, instead of writing the connection code above.

Read the calling docs · Start building free

Frequently asked questions

Do I need a TURN server for WebRTC?

For production, yes. Most calls connect directly with STUN, but users behind restrictive NATs or firewalls can't connect at all without a TURN relay.

What is the difference between STUN and TURN?

STUN tells a device its public address so peers can connect directly. TURN relays the media when a direct connection fails. STUN is cheap; TURN carries the full call bandwidth.

What is ICE in WebRTC?

Interactive Connectivity Establishment. It gathers possible addresses from the device, STUN and TURN, tests them in pairs and picks the best path that works.

Which ports do STUN and TURN use?

The default is 3478 over UDP or TCP, and 5349 for TLS. Many deployments also run TURN over TLS on port 443 so it passes through corporate firewalls.

Can I use a free public STUN server?

For testing, yes. Public STUN servers come with no uptime guarantee, and no one offers free TURN at scale because relaying costs bandwidth. Run your own or use your provider's for production.

Related terms