
WebRTC & Its Protocols
The blog walks through all the core protocols in the WebRTC stack — SDP, ICE, STUN, TURN, DTLS, SRTP, and SCTP — explaining what each one does and why it exists.
Bharat
Author
WebRTC — short for Web Real-Time Communication — is an open standard that enables browsers and mobile apps to exchange audio, video, and data directly with each other, without needing a plugin or intermediary server for the media stream itself.
It powers video calls, screen sharing, peer-to-peer file transfers, and live gaming — all natively, straight from JavaScript.
Workflow
#How Does a WebRTC Connection Work?
At its core, WebRTC follows a three-phase handshake:
- Signaling — peers discover each other and exchange connection metadata
- NAT Traversal — peers find a viable network path to each other
- Media/Data Transport — actual audio, video, or data flows between peers
Each phase is handled by a specific set of protocols.
#How I Applied These Concepts in My Project
I built ShareLink, a browser-based file sharing tool inspired by WebRTC principles.
Try it Yourself:
#The Protocol Stack
1. Signaling
WebRTC deliberately leaves signaling open-ended. You can use WebSockets, HTTP, SIP, or anything you like to exchange:
- SDP (Session Description Protocol) — describes the media capabilities of each peer (codecs, bandwidth, encryption keys, etc.)
- ICE candidates — network address/port pairs to try for connection
WebRTC specifies what must be exchanged, not how. This flexibility is intentional.
2. ICE — Interactive Connectivity Establishment
ICE is the framework that finds the best network path between two peers, especially through NATs and firewalls.
It works by gathering a list of candidates — possible network endpoints — and testing them:
| Candidate Type | Description |
|---|---|
| Host | Local IP addresses on the device |
| Server Reflexive | Public IP as seen by a STUN server |
| Relay | Traffic routed through a TURN server |
ICE tries candidates in priority order and picks the best working pair.
3. STUN — Session Traversal Utilities for NAT
STUN is a lightweight protocol that helps a peer discover its public IP address and port, as seen from the internet.
Client → STUN Server: "What's my public IP?"
STUN Server → Client: "You appear as 203.0.113.45:54321"
This works great when both peers are behind simple NATs. For symmetric NATs or strict firewalls, you need TURN.
4. TURN — Traversal Using Relays around NAT
When direct peer-to-peer fails, TURN acts as a relay server — traffic flows through it instead of directly between peers.
Peer A ──► TURN Server ──► Peer B
TURN is bandwidth-heavy (since all media passes through it), but it's the fallback that ensures connectivity in restrictive networks.
5. DTLS — Datagram Transport Layer Security
WebRTC encrypts everything. DTLS is TLS adapted for UDP — it performs a handshake to exchange keys and establish a secure channel before any media or data is sent.
- All WebRTC connections must be encrypted
- DTLS protects both data channels and media key exchange
6. SRTP — Secure Real-time Transport Protocol
Audio and video are carried over SRTP, which is RTP with encryption and message authentication baked in.
- RTP handles the actual packetization and sequencing of media
- RTCP (companion protocol) provides statistics and feedback (jitter, packet loss, round-trip time)
- SRTP wraps RTP with DTLS-negotiated keys for confidentiality
7. SCTP — Stream Control Transmission Protocol
For data channels (non-media peer-to-peer data), WebRTC uses SCTP tunneled over DTLS.
SCTP offers flexibility that neither TCP nor UDP alone provides:
- Reliable or unreliable delivery (configurable per channel)
- Ordered or unordered messages
- Multi-streaming — multiple logical channels over one connection
This makes it perfect for use cases like file transfer, gaming state sync, or chat.
#The Full Stack at a Glance
┌─────────────────────────────────────┐
│ Your Application (JS) │
├──────────────┬──────────────────────┤
│ MediaStream │ RTCDataChannel │
├──────────────┼──────────────────────┤
│ SRTP │ SCTP │
├──────────────┴──────────────────────┤
│ DTLS │
├─────────────────────────────────────┤
│ ICE (STUN / TURN) │
├─────────────────────────────────────┤
│ UDP │
└─────────────────────────────────────┘
#Quick Summary
| Protocol | Role |
|---|---|
| SDP | Describes session capabilities |
| ICE | Finds the best network path |
| STUN | Discovers public IP/port |
| TURN | Relays traffic when P2P fails |
| DTLS | Encrypts the connection |
| SRTP | Carries encrypted audio/video |
| SCTP | Powers the data channel |
#Closing Thoughts
WebRTC is a remarkably layered, resilient system. Each protocol solves a distinct problem — NAT traversal, encryption, media transport, data reliability — and together they make seamless real-time communication possible across nearly any network condition.
If you're building video calls, collaborative tools, or peer-to-peer apps, understanding this stack gives you the mental model to debug issues, optimize performance, and make smarter architectural decisions.
Published on 5/3/2026