Home/Blogs/WebRTC NAT Traversal
Networking & P2P

How WebRTC NAT Traversal Works: STUN, TURN, and ICE Explained

By Crate Editorial Team•Published Oct 24, 2026•10 min read

When you transfer a 4GB raw video file directly between two laptops across town using Crate Drop, or duel another player in real-time on TypeCrate, data packets fly straight from one browser to the other. No cloud storage, no intermediate upload step, and zero server bandwidth fees. But on the modern internet, two devices cannot simply dial each other’s internal IP address (`192.168.1.45`). Between them sits Network Address Translation (NAT) and strict enterprise firewalls.

Understanding how WebRTC punctures these digital walls requires demystifying the three pillars of WebRTC connectivity:STUN, TURN, and ICE.

Advertisement
Google AdSense Partner SlotActive upon AdSense domain verification

The Problem: The NAT Wall

IPv4 addresses are scarce. To solve this, your home Wi-Fi router assigns local private IP addresses to your phone, laptop, and smart TV. When your laptop sends an outbound packet to `google.com`, your router rewrites the source IP and port from your local address to its own public WAN IP.

This works seamlessly for client-server communication because the client initiates the request. But for Peer-to-Peer (P2P), Device A and Device B do not know each other’s public IP or port. Worse, if Device B attempts to send an unsolicited packet to Device A’s public IP, Device A’s router drops it immediately for security.

1. STUN: The Mirror in the Cloud

STUN (Session Traversal Utilities for NAT, RFC 5389) is an extremely lightweight protocol designed to answer one simple question:"What is my public IP address and port as seen by the outside world?"

  • Device A sends a STUN binding request to a known public STUN server (such as Google’s free `stun:stun.l.google.com:19302`).
  • The STUN server inspects the incoming UDP packet header, notes the public IP and source port allocated by Device A’s router, and replies back with that mapped address (the XOR-MAPPED-ADDRESS).
  • Now Device A knows its public persona and can share it with Device B over a signaling channel.
// WebRTC ICE Server Configuration
const rtcConfig = { iceServers: [ { urls: 'stun:stun.l.google.com:19302' }, { urls: 'turn:turn.cratetools.me:3478', username: 'p2p_user', credential: 'secure_auth_token' } ] };

2. The Failure of STUN: Symmetric NATs

STUN succeeds in approximately 80% to 85% of consumer home network situations. However, it fails completely on Symmetric NATs, commonly found on cellular 4G/5G networks, university campuses, and corporate VPNs.

Under symmetric NAT, the router assigns a unique external port for every individual destination IP and port requested. When Device A contacts the STUN server, port `45231` is allocated. But when Device A subsequently attempts to send packets to Device B, the router assigns a completely new, unpredictable port (`58190`). Because Device B was expecting communication on port `45231`, direct hole punching fails.

3. TURN: The Guaranteed Relay Fallback

When direct P2P hole punching is impossible, WebRTC falls back to TURN (Traversal Using Relays around NAT, RFC 5766).

A TURN server is not a signaling mirror; it is an active media relay. Both peers establish an outbound connection to the TURN server, which forwards all encrypted packets between them. While this eliminates the cost savings of zero-bandwidth P2P, it ensures a 100% connection success rate regardless of how strict the firewall or enterprise proxy might be.

4. ICE: The Master Orchestrator

ICE (Interactive Connectivity Establishment, RFC 8445) is the algorithm that brings STUN and TURN together. Instead of guessing which connection method will work, ICE gathers every conceivable communication path—called ICE Candidates:

Candidate TypeSourceLatency / Priority
hostLocal NIC (LAN/Wi-Fi internal IP)Highest (< 1ms on local network)
srflx (Server Reflexive)Public IP/port discovered via STUNDirect P2P over WAN
prflx (Peer Reflexive)Discovered dynamically during checksDirect P2P alternate route
relayRelayed via TURN serverFallback (Higher latency, guaranteed)

The browsers exchange these candidates via SDP (Session Description Protocol) offers and answers through a signaling server (such as WebSockets). ICE performs connectivity checks in parallel, pairs the best working paths, and locks onto the lowest-latency route.

How We Apply This in Crate Tools

In Crate Drop, ICE pairing allows two devices in the same office to transfer gigabytes of 4K footage at local Gigabit Wi-Fi speeds without touching the public internet at all. In TypeCrate, host and srflx candidates provide sub-20ms keystroke synchronization for head-to-head typing battles.

Advertisement
Google AdSense Partner SlotActive upon AdSense domain verification
Crate Creative Suite

More Free Creator Tools

100% private, client-side tools designed for video editors and creators.

Popular

Crate Captions

Auto-generate gapless 1w–5w word subtitles with smart emojis for CapCut, Premiere, & Shorts.

Video & SubtitlesLaunch →

Video Compressor

Compress MP4 and MOV videos up to 85% using in-browser WebAssembly with zero quality loss.

Video & SubtitlesLaunch →

Extract Audio

Rip clean uncompressed WAV audio from any MP4, MOV, or WebM video locally in seconds.

Audio UtilitiesLaunch →

Compress Image

Batch optimize JPEG, PNG, and WebP images client-side with interactive before/after inspection.

Image ToolsLaunch →

Remove Background

Instant neural foreground isolation. Create transparent PNG cutouts without server uploads.

Image ToolsLaunch →

Convert Image

Bulk convert WebP to PNG/JPG, PNG to WebP/JPG, or JPG to WebP with one-click ZIP download.

Image ToolsLaunch →

Resize Image

Batch resize photos by pixel dimensions or percentages with aspect ratio lock and ZIP export.

Image ToolsLaunch →

Crop Image

Crop images visually with 16:9, 1:1, 9:16, and 4:5 social presets with zero quality loss.

Image ToolsLaunch →

Rotate Image

Batch rotate images 90°, 180°, or 270° and mirror flip horizontally or vertically.

Image ToolsLaunch →

Watermark Image

Batch stamp copyright text or branding across photos with 9-point grid and diagonal repeat.

Image ToolsLaunch →

Blur & Censor Image

Blur faces, pixelate sensitive personal info, or blackout credit card numbers 100% in-browser.

Image ToolsLaunch →

Meme Generator

Create viral memes with classic Impact captions, custom image upload, and zero watermarks.

Image ToolsLaunch →

Crate Drop

Stream gigabyte files directly between two devices over encrypted peer-to-peer Wi-Fi channels.

File TransferLaunch →
Popular

Font Style Changer

Transform text into 50+ aesthetic Unicode fonts for Instagram bios, TikTok captions, & Discord.

Text & TypographyLaunch →