Skip to content

WebRTC Architecture Deep Dive

Understand WebRTC's peer-to-peer architecture, media engines, and the SDP/ICE negotiation process.

What you'll learn

  • WebRTC peer-to-peer vs client-server model
  • The three WebRTC APIs: MediaStream, RTCPeerConnection, RTCDataChannel
  • SDP offer/answer negotiation
  • ICE, STUN, and TURN for NAT traversal

Concept

WebRTC Architecture Deep Dive

WebRTC enables peer-to-peer audio, video, and data transfer between browsers without plugins, reducing server bandwidth.

Peer-to-Peer Model

Unlike WebSocket (client-to-server), WebRTC connects browsers directly. This reduces server bandwidth but requires NAT traversal via STUN/TURN.

Three APIs

MediaStream (getUserMedia) accesses camera/mic. RTCPeerConnection handles media transport and NAT traversal. RTCDataChannel provides P2P data transfer.

SDP Offer/Answer

Before media flows, peers negotiate capabilities using SDP. Peer A creates an offer, Peer B creates an answer. This exchange happens via a signaling server.

ICE, STUN, TURN

ICE finds the best network path. STUN helps peers discover public IPs. TURN relays traffic when P2P fails. Always configure TURN - 10-20% of connections need it.

Example

              const pc = new RTCPeerConnection({iceServers:[{urls:'stun:stun.l.google.com:19302'},{urls:'turn:turn.example.com',username:'u',credential:'p'}]});
pc.onicecandidate = (e) => { if (e.candidate) console.log('ICE:', e.candidate.candidate.slice(0,50)); };
pc.ontrack = (e) => console.log('Remote stream:', e.streams[0].id);
const dc = pc.createDataChannel('chat');
dc.onopen = () => dc.send('Hello via data channel!');
console.log('PeerConnection created with', 2, 'ICE servers');
            

RTCPeerConnection is configured with STUN and TURN servers. ICE candidates are exchanged via signaling. Data channel enables P2P data transfer.

Try it

  • JSON Formatter

    SDP offers and ICE candidates are often logged as JSON for debugging.

Common mistakes

The mistake

Not configuring a TURN server.

The fix

About 10-20% of connections need TURN relay. Without it, those users cannot connect.

The mistake

Not handling ICE failure.

The fix

ICE can fail to find a path. Handle iceConnectionStateChange and show clear errors.

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors