STUN and TURN are the two signaling-adjacent protocols that make WebRTC peer-to-peer connections possible across NATs and firewalls. STUN lets a client discover its public IP address and port so peers can connect directly, while TURN acts as a media relay when direct paths fail, typically for 5 to 20 percent of users behind symmetric NATs or strict corporate firewalls. Managed platforms like VideoSDK abstract this entire layer, running STUN/TURN infrastructure so your video calling app connects reliably without you operating servers. For those building on raw WebRTC, this guide covers how both protocols work and how to deploy them yourself with coturn.
Every WebRTC developer eventually hits the same wall: two peers that can exchange signaling messages perfectly but cannot send a single audio packet to each other. The culprit is almost always network address translation. Most devices sit behind at least one NAT layer, whether a home router, a carrier-grade NAT, or a corporate firewall, and none of them make it easy for an unsolicited UDP packet to arrive from the internet.
STUN and TURN exist to solve exactly this problem. STUN (Session Traversal Utilities for NAT) helps a peer discover how it appears from the outside so direct connections can be attempted. TURN (Traversal Using Relays around NAT) provides a fallback relay when direct paths are impossible. Together with ICE (Interactive Connectivity Establishment), they form the connectivity backbone of every real-time audio, video, and data session on the web today.
If you are building real-time communication features, understanding these protocols is not optional trivia. Connection success rates, latency, and infrastructure cost all trace back to how well your STUN/TURN layer is designed. And if you would rather not run that layer at all, a managed service like VideoSDK's video calling API handles it for you.

What Are STUN and TURN?

STUN is defined as a lightweight request-response protocol that lets a client discover its public IP address and port as seen by an external server. TURN is defined as a protocol that allocates a relay address on a server, through which all media flows when peers cannot connect directly. Both operate over UDP or TCP, and both are standardized by the IETF: STUN in RFC 5389 (later revised in RFC 8489) and TURN in RFC 5766, with extensions for transport relays over TCP and TLS.
STUN emerged in the early 2000s as middleware boxes proliferated and direct peer-to-peer communication broke. TURN followed once it became clear that a meaningful fraction of NAT configurations could never be traversed by discovery alone. The two protocols were later folded into the ICE framework (RFC 8445), which WebRTC uses to orchestrate candidate gathering and connectivity checks.
The practical difference is cost. A STUN transaction is a single round trip, a few hundred bytes, and then the server is out of the picture. A TURN relay carries every media packet for the entire session, which means bandwidth, CPU, and money. That asymmetry shapes every deployment decision that follows.

Why STUN and TURN Matter for WebRTC

Connection success rate is the single most visible metric in any WebRTC product, and it is governed almost entirely by NAT traversal. In practice, direct peer-to-peer paths succeed for roughly 80 to 95 percent of users when STUN is available. The remaining minority, typically users behind symmetric NATs, mobile carriers with aggressive NAT behavior, or corporate networks blocking UDP, simply cannot connect without a TURN relay.
Skipping TURN does not save money; it loses users. A telehealth consultation that fails to connect is a missed appointment. A live tutoring session that drops is a refund. Latency matters too: direct paths are fastest, but a well-placed TURN relay at 30 to 60 milliseconds of added round-trip time beats no connection at all. Managed RTC platforms like VideoSDK treat this as a solved problem, operating geo-distributed TURN infrastructure with automatic fallback so every participant connects.

How STUN Works: Public IP Discovery

STUN works by having a client send a binding request to a publicly reachable STUN server and reading the source address of that request as observed by the server. The server echoes that observed address back in the response. The client now knows its public IP and port, called the server-reflexive candidate, and offers it to the remote peer during signaling.
The clever part is what STUN reveals about NAT behavior. If a client sends two binding requests from the same internal port to two different STUN servers and receives two different public endpoints, the NAT is symmetric: it assigns a new external mapping per destination. That mapping is useless for inbound connections from a peer, which is precisely why symmetric NATs force a TURN fallback. If both servers report the same mapping, the NAT is likely cone-type and direct connection has a good chance.
STUN servers are stateless and cheap. Public STUN servers operated as part of open projects are fine for development, but production deployments should use dedicated or self-hosted instances to avoid rate limits and unpredictable availability.
Architecture Diagram

How TURN Works: Media Relay

TURN is invoked only after ICE determines that no direct candidate pair works. The client authenticates against the TURN server, usually with short-lived credentials, and requests an allocation. The server grants a relayed address, which the client then advertises as a relay candidate to its peer. All subsequent media flows through that allocation: client to TURN server, TURN server to peer.
Because the relay sits in the media path, it doubles bandwidth on the server side (inbound plus outbound for every stream) and adds one network hop of latency. Placement matters enormously. A TURN server in a different continent can add 150 milliseconds or more to every packet, which is why production deployments run multiple regional relays and let clients connect to the nearest one.
TURN also supports permissions, which restrict which remote addresses may send to an allocation, and channel binding, which compresses the per-packet overhead for long-lived flows. Both matter at scale. For most teams, the honest cost calculation is simple: TURN bandwidth is the price of universal connectivity, and it should be budgeted per concurrent relayed participant, not per registered user.
Architecture Diagram

NAT Types and Their Effect on STUN/TURN Success

NAT behavior falls into four broad categories, and each determines whether STUN alone can succeed. Full-cone NATs map an internal endpoint to one public endpoint and accept inbound packets from any external host, making them the easiest to traverse. Restricted-cone NATs accept inbound packets only from addresses the client has already contacted, which ICE handles through its ordered connectivity checks. Port-restricted NATs add the constraint that inbound packets must come from the exact IP and port the client contacted, still traversable by ICE in most cases.
Symmetric NATs are the hard case. They create a unique external mapping for every destination, so the public address discovered via STUN is not valid for the peer's traffic. Direct connection fails, and TURN is the only option. Carrier-grade NAT on mobile networks frequently behaves symmetrically, which is why mobile apps see noticeably higher relay rates than desktop apps on home broadband.
The practical takeaway: you cannot control your users' NATs, so your infrastructure must assume the worst case. A deployment with STUN but no TURN will silently fail for a slice of your user base, and that slice will disproportionately include mobile users, the audience most products care about most.

Choosing Between STUN and TURN

The decision is not either/or; it is about layering. Use STUN always, because it is free and enables the fast direct path for the majority of connections. Use TURN always as a fallback, because the minority who need it have no alternative. The real engineering decisions are about TURN capacity, placement, and cost control.
  • Direct path sufficient: both peers on cone-type NATs, residential or office networks allowing UDP, same-region peers. STUN discovery plus ICE checks will connect them with minimal latency.
  • TURN required: either peer behind symmetric or carrier-grade NAT, UDP blocked by firewall policy, cross-network calls where only TCP or TLS relaying is permitted, or strict enterprise networks.
  • Managed alternative: if operating regional TURN fleets, credential services, and monitoring sounds like overhead you would rather avoid, platforms like VideoSDK include the full traversal stack as part of the SDK.

Deploying a STUN/TURN Server with coturn

coturn is the open-source reference implementation used for the majority of self-hosted STUN/TURN deployments. Deploying it well involves several distinct stages, each with production considerations.
Provision the server. Choose a virtual machine in a region close to your users, with a public IP address, generous bandwidth allowance, and UDP ports open. TURN servers are bandwidth-bound, so network throughput matters more than CPU for small deployments.
Install and configure coturn. After installing the package, the core configuration decisions are: enable both STUN and TURN listeners, define the listening port range for relay allocations (a few hundred ports is typical for moderate concurrency), and set the external IP so the server advertises the correct public address.
Set up authentication. Never run an open TURN server; it will be found and abused as a traffic relay within days. Use time-limited credentials generated by your backend using a shared secret, so each client receives a username encoding an expiry timestamp and a matching password. This is the same token-based pattern VideoSDK uses for room authentication, applied to traversal.
Add TLS. For clients restricted to TCP 443, configure TLS listeners with a valid certificate for your TURN domain. Many corporate networks only allow TLS traffic, so this listener is what rescues those users.
Create DNS records. Give the server a stable hostname, and ideally use DNS-based geographic routing so clients in different regions resolve to their nearest relay.
Plan for high availability. A single TURN server is a single point of failure for every relayed session. Run at least two nodes behind a load balancer or DNS round-robin, and make sure your client configuration lists multiple servers so ICE can fail over during candidate gathering.
Architecture Diagram

Testing and Monitoring Your STUN/TURN Setup

Testing starts in the browser. Chrome's built-in WebRTC internals page (accessible via the chrome://webrtc-internals URL) shows every ICE candidate gathered, which candidate pair was selected, and whether the final path was direct (server-reflexive) or relayed. This single page answers most "why won't they connect" questions in seconds: if no relay candidates appear, your TURN configuration is broken; if relay candidates appear but checks fail, credentials or firewall rules are the suspects.
For ongoing monitoring, track four metrics. First, relay percentage: the share of sessions using TURN, which should typically sit between 5 and 20 percent and spikes signal network changes in your user base. Second, connection setup time from ICE start to first media packet. Third, relay bandwidth per node, which drives capacity planning. Fourth, allocation failure rate, which exposes credential or port exhaustion problems. Log analysis on the coturn side complements this, revealing authentication failures and allocation churn.

Common Pitfalls and Troubleshooting

Most STUN/TURN failures cluster into a handful of patterns. Missing relay candidates in the browser's ICE dump almost always mean the TURN server is unreachable, the URL scheme is wrong for the transport being used, or the server's certificate is invalid for TLS listeners. Verify the server responds on the expected ports from a client on a different network.
Credential mismatches produce authentication failures in coturn logs. The classic cause is clock skew: time-limited usernames encode an expiry, so a server or client with a wrong system clock rejects valid credentials. Keep clocks synchronized via NTP on both sides.
Firewall blocks on the server side are the silent killer. The listening port must be open, but so must the entire relay port range, and some cloud providers apply stateful firewall rules that interfere with long-lived UDP allocations. Finally, advertising a private or incorrect external IP makes the server hand out unusable relay addresses; always confirm the external address configuration matches the machine's actual public IP.

Best-Practice Checklist

  • Always configure both STUN and TURN; never ship one without the other.
  • Use short-lived, backend-generated TURN credentials rather than static passwords.
  • Deploy at least two TURN nodes in separate regions with DNS-based client routing.
  • Enable TLS listeners on port 443 for firewall-restricted clients.
  • Monitor relay percentage, allocation failures, and per-node bandwidth.
  • Test from real mobile networks, not just office broadband, before launch.
  • Consider a managed RTC platform like VideoSDK if operating traversal infrastructure is not your core competency.

Definitions Glossary

STUN (Session Traversal Utilities for NAT): A lightweight request-response protocol that lets a WebRTC client discover its public IP address and port as observed by an external server, enabling direct peer-to-peer connections through cone-type NATs.
TURN (Traversal Using Relays around NAT): A protocol that allocates a relay address on a public server, forwarding all media between peers when direct connection is impossible, typically needed for 5 to 20 percent of users.
ICE (Interactive Connectivity Establishment): The WebRTC framework that gathers STUN and TURN candidates, tests candidate pairs in priority order, and selects the first working path for media flow.
Server-Reflexive Candidate: The public IP and port a client learns from a STUN response, representing its NAT mapping and offered to the remote peer during signaling.
Symmetric NAT: A NAT that creates a unique external mapping for every destination, invalidating STUN-discovered addresses and forcing a TURN relay fallback.
coturn: The widely used open-source STUN/TURN server implementation that powers most self-hosted WebRTC traversal deployments.

Key Takeaways

  • STUN enables direct connections by revealing a client's public address, while TURN relays media when direct paths fail; production WebRTC needs both.
  • Symmetric NATs and UDP-blocking firewalls make TURN mandatory for roughly 5 to 20 percent of users, disproportionately on mobile networks.
  • TURN servers sit in the media path, so regional placement, bandwidth budgeting, and TLS support on port 443 are core production requirements.
  • Browser ICE diagnostics and coturn logs resolve most failures, which usually trace to credentials, firewall rules, or misconfigured external addresses.
  • Managed platforms like VideoSDK include the full STUN/TURN traversal stack, geo-distributed relays, and automatic fallback, removing the operational burden entirely.

Conclusion

STUN and TURN are the quiet backbone of every reliable WebRTC experience: STUN wins the fast direct path for most users, and TURN rescues the rest. Getting them right means paired deployment, short-lived credentials, regional relays, TLS fallback, and continuous monitoring of relay rates. If operating that infrastructure yourself is more than you want to carry, VideoSDK's video calling SDK ships with the complete traversal layer built in, alongside recording, transcription, and adaptive streaming. You can start free at app.videosdk.live/login and explore code samples for your platform. What are you building with WebRTC? Drop a comment, I'd love to hear what kind of real-time use case you're working on.

Free $20 Balance for AI Voice Agents & Video Calls

FAQ