# STUN

> The STUN (Session Traversal Utilities for NAT) is a protocol that lets a device discover its public IP address and port.

## What is a STUN server?

The protocol a device uses to ask a server which public IP address and port it is visible from behind a router. This address becomes an ICE candidate and allows a direct connection in most home networks. STUN servers are cheap because they do not relay media.

- Topic: Real-Time Communication
- Type: protocol

## Example

Wrong:

```js
const pc = new RTCPeerConnection({
  iceServers: [{ urls: 'stun.example.com:3478' }],
});
```

Right:

```js
const pc = new RTCPeerConnection({
  iceServers: [{ urls: 'stun:stun.example.com:3478' }],
});
```

ICE server URLs need the 'stun:' scheme; without it the constructor throws an error.

## Common mistakes

- Assuming STUN alone is enough for corporate or mobile networks.

## Key points

- A STUN server is not the only ICE server: a TURN fallback is also configured.
- STUN URLs in the config use the 'stun:' scheme and a reachable server.

## Interview questions

### What is the difference between STUN and TURN?

STUN only tells a device its public address so peers can connect directly. TURN relays all traffic when a direct path is impossible.

### Which port does STUN use?

Port 3478 over UDP or TCP by default, and port 5349 for STUN over TLS.

### Can I use a free public STUN server in production?

It works, but public servers have no uptime guarantee. Production apps usually run their own or use a provider together with TURN.

## Related cards

- [ICE](https://wonderium.dev/web/ice.md)
- [TURN](https://wonderium.dev/web/turn.md)
- [IP Address](https://wonderium.dev/web/ip-address.md)

---

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