Prompt
How do I set up TURN and STUN for video calls?
Latest observation
Nov 2, 2026
OpenAI APIWeb search: off
To set up STUN and TURN for video calls, you’re usually configuring WebRTC so peers can find each other and relay media when direct connectivity fails.
Quick concept
- STUN: helps clients discover their public IP/port.
- TURN: relays media when direct peer-to-peer connection is blocked by NAT/firewalls.
- In production, you typically need both:
- STUN for direct connections when possible
- TURN as a fallback for reliability
1) Choose a TURN/STUN server
You can:
- Use a managed provider
- Self-host with coturn (most common open-source option)
If you want full control, coturn is a good choice.
2) Install coturn (example on Ubuntu/Debian)
sudo apt update
sudo apt install coturn
Enable the service:
sudo nano /etc/default/coturn
Set:
TURNSERVER_ENABLED=1
3) Basic coturn config
Edit:
sudo nano /etc/turnserver.conf
Example config:
listening-port=3478
tls-listening-port=5349
fingerprint
lt-cred-mech
realm=yourdomain.com
server-name=yourdomain.com
use-auth-secret
static-auth-secret=YOUR_LONG_RANDOM_SECRET
total-quota=100
bps-capacity=0
no-multicast-peers
no-loopback-peers
# Recommended for production behind NAT:
external-ip=YOUR_PUBLIC_IP
Notes
realmshould match your domainstatic-auth-secretis used for temporary credentialsexternal-ipis important if the server is behind NAT- You’ll usually need both UDP and TCP open, and TLS if possible
4) Open firewall ports
At minimum:
- 3478/udp and 3478/tcp for TURN/STUN
- 5349/tcp for TURN over TLS
- A relay port range for media relaying
Example with ufw:
sudo ufw allow 3478/udp
sudo ufw allow 3478/tcp
sudo ufw allow 5349/tcp
sudo ufw allow 49152:65535/udp
You can restrict the relay range in coturn:
min-port=49152
max-port=65535
Then only open that range.
5) Start/restart coturn
sudo systemctl restart coturn
sudo systemctl enable coturn
Check status:
sudo systemctl status coturn
6) Configure your WebRTC app
In your client code, pass ICE servers to RTCPeerConnection.
Example:
const pc = new RTCPeerConnection({
iceServers: [
{ urls: 'stun:yourdomain.com:3478' },
{
urls: 'turn:yourdomain.com:3478',
username: 'user',
credential: 'pass'
},
{
urls: 'turns:yourdomain.com:5349',
username: 'user',
credential: 'pass'
}
]
});
7) Use TURN authentication properly
For production, don’t hardcode static usernames/passwords in the client.
Better approach:
- Your backend generates short-lived TURN credentials
- The client fetches them just before starting a call
If using use-auth-secret, generate ephemeral credentials using the shared secret.
Typical pattern:
- username = expiration timestamp
- password = HMAC-SHA1 of username using
static-auth-secret
8) Test connectivity
Useful checks:
- Verify STUN works: public IP discovery
- Verify TURN relay works when direct path fails
- Use browser WebRTC stats or tools like:
chrome://webrtc-internalscoturnlogs
You can also test with:
trickle-iceWebRTC ICE testerturnutils_uclientfrom coturn
9) Common pitfalls
- Firewall not opening relay ports
- Wrong public IP / NAT config
- Not using TLS on restrictive networks
- Using static credentials in client code
- Forgetting TCP/TLS fallback
- Binding TURN only to localhost
- DNS not pointing to server correctly
10) Recommended production setup
A practical setup looks like this:
stun:yourdomain.com:3478turn:yourdomain.com:3478?transport=udpturn:yourdomain.com:3478?transport=tcpturns:yourdomain.com:5349?transport=tcp
And:
- coturn with long-term credentials or auth-secret
- TLS certificate from Let’s Encrypt
- relay port range restricted and firewall-opened
- backend-generated ephemeral TURN creds
If you want, I can give you:
- a working coturn config file,
- a Node.js backend to generate TURN credentials, or
- a full WebRTC example with STUN/TURN setup.