# TURN

> The TURN (Traversal Using Relays around NAT) is a protocol for relaying media through a server when a direct connection is impossible.

## What is a TURN server and why is it needed?

The protocol that relays traffic through a server when peers cannot connect directly, for example behind strict firewalls. Guarantees that calls connect, but costs bandwidth because all media passes through the server.

- Topic: Real-Time Communication
- Type: protocol

## Example

Wrong:

```js
const pc = new RTCPeerConnection({
  iceServers: [{
    urls: 'turn:turn.example.com:3478',
    username: 'admin',
    credential: 'secret123',
  }],
});
```

Right:

```js
const res = await fetch('/api/turn-credentials');
const { iceServers } = await res.json();
const pc = new RTCPeerConnection({ iceServers });
```

The backend issues short-lived TURN credentials per session, so nothing reusable is shipped in client code.

## Common mistakes

- Publishing permanent TURN credentials in client code, letting anyone use the relay.

## Key points

- TURN credentials are generated on the server and expire, they are not hardcoded in the frontend.
- TURN over TLS on port 443 is available for networks that block other ports.

## Interview questions

### How do short-lived TURN credentials work?

The backend builds a username with an expiry time and signs it with an HMAC using a secret shared with the TURN server, which checks both.

### Can a TURN server see my call?

No. It relays packets that WebRTC encrypts between the peers, so the relay cannot decrypt the audio or video.

### How many calls need TURN?

Usually a minority, mostly on corporate or mobile networks with strict NAT. Without TURN those calls simply fail.

## Related cards

- [STUN](https://wonderium.dev/web/stun.md)
- [ICE](https://wonderium.dev/web/ice.md)
- [WebRTC](https://wonderium.dev/web/webrtc.md)

---

- Level: [Web Platform](https://wonderium.dev/web.md)
- HTML version: https://wonderium.dev/web/turn
- Last updated: 2026-11-25
