SDSystem DesignConcepts to case studies
System DesignDSA
x
0/311
Part 1: Foundations
Part 2: APIs and Communication
  • Long Polling
  • Server-Sent Events (SSE)
  • WebSockets
  • Webhooks
  • WebRTC
  • Short Polling
  • Push Notifications
  • MQTT
Part 3: Caching
Part 4: Data and Storage
Part 5: Architecture
Part 6: Distributed Systems
Part 7: Advanced Data Topics
Part 8: Operations and Security
Part 9: Estimation and Interview Method
Part 10: Case Studies
System Design/APIs and Communication/Real-Time Communication

WebRTC

Video, voice and data sent straight between two browsers.

โฑ 14 min readAdvancedHas visualizer
SummaryConceptReal-Life ExampleWhy?Where?How It WorksStepsKey TermsPros & ConsMistakesReal WorldRead NextInterview Q&ACheat-Sheet

01Quick Summary

WebRTC (Web Real-Time Communication) lets two browsers send video, voice and data directly to each other, without sending it all through your server.

  • It is built into modern browsers and phones. No plug-in needed.
  • Your server is only needed to introduce the two people (signalling) and to help in hard network cases (STUN and TURN).
  • Media is always encrypted.
  • It powers video calls, screen sharing, live streams and peer-to-peer file transfer.

02Concept: What is it?

With normal web apps, everything goes through a server. For a video call that is slow and expensive. WebRTC lets the two browsers connect peer to peer.

Through a server vs peer to peer
Relay everything through a server
๐Ÿ‘ฉAsha
โ†•all video
๐Ÿ–ฅ๏ธYour serverpays for all traffic
โ†•all video
๐Ÿ‘จRavi
WebRTC peer to peer
๐Ÿ‘ฉAsha
โ†•๐Ÿ”’ direct audio + video
๐Ÿ‘จRavi
๐Ÿ›ฐ๏ธSignalling serveronly introduces them
Costly path Direct encrypted path Helper server
Direct is faster and cheaper. The server only helps them meet.

What WebRTC gives you

๐ŸŽฅVideo and audio
Camera and microphone streams (MediaStream).
๐Ÿ–ฅ๏ธScreen sharing
Share a window or the whole screen.
๐Ÿ“จData channels
Send any text or binary data directly between peers.

03Real-Life Example

๐ŸตTwo friends and a mutual friend
Asha and Ravi want to talk but do not have each other's number. A mutual friend (the signalling server) passes their numbers across. Then they call each other directly. If their phones cannot connect (blocked networks), they use a relay operator (TURN) who passes the call along.
Calling a friend
WebRTC
Mutual friend who shares numbers
Signalling server
Describing what phone you have
SDP offer and answer
Asking 'what is my public number?'
STUN server
Possible numbers to try
ICE candidates
Calling directly
Peer-to-peer connection
Operator who connects both when direct fails
TURN relay
Speaking in a private code
DTLS + SRTP encryption
A video call, end to end
๐Ÿ‘ฉAshacamera + mic
โ†’โ†“1. signalling (WebSocket)
๐Ÿ›ฐ๏ธSignalling server
โ†1. signalling
๐Ÿ‘จRavicamera + mic
๐Ÿ‘ฉAsha
โ†”2. ๐Ÿ”’ media, direct
๐Ÿ‘จRavi

04Why Do We Need It?

Video is heavy. Sending it all through your servers means high cost and extra delay. WebRTC fixes this:

โšกLow delay
A direct path means the shortest trip. Calls feel live.
๐Ÿ’ธLower cost
Media does not touch your servers (most of the time).
๐Ÿ”’Encrypted by design
DTLS and SRTP are required. There is no 'unencrypted mode'.
๐ŸŒNo plug-ins
Works in Chrome, Safari, Firefox and Edge, and in mobile apps.
๐Ÿ“ถAdapts to bad networks
It lowers video quality when bandwidth drops.
๐Ÿ“จData too
Data channels send game moves or files with no server in the middle.
Use WebRTC when people need to hear and see each other live, or exchange data directly.

05Where Do We Use It?

๐Ÿ“นVideo calls
One-to-one calls and small meetings.
๐ŸŽ™๏ธVoice chat
Game voice, support calls in the browser.
๐Ÿ–ฅ๏ธScreen sharing
Demos, remote help, classrooms.
๐ŸŽฎCloud and P2P games
Fast data channels between players.
๐Ÿ“File transfer
Send a large file between two browsers directly.
๐Ÿ“กLive streaming
Ultra-low-delay broadcasts, often with a media server.

06Types / Parts / How It Works

The pieces

PieceJob in easy words
SignallingHow the two peers swap setup notes. WebRTC does NOT define it. You build it, often with WebSockets
SDPThe text note: 'here are my codecs, media and security fingerprint'
ICEThe process that tries many network routes and picks the best one
STUNA small server that tells a browser its public address
TURNA relay server used when a direct link is impossible
DTLSHandshake that agrees on encryption keys
SRTPEncrypted audio and video packets
Data channel (SCTP)A direct pipe for any data, reliable or not
CodecHow audio and video are compressed (for example Opus, VP8, H.264)

The setup flow

  1. 1
    Get camera and microphone
    getUserMedia() asks the user for permission.
  2. 2
    Create a peer connection
    new RTCPeerConnection({ iceServers: [STUN, TURN] }).
  3. 3
    Offer and answer
    Asha creates an SDP offer. Ravi replies with an SDP answer. Both go through signalling.
  4. 4
    Swap ICE candidates
    Each side lists possible addresses and sends them over signalling.
  5. 5
    ICE picks a path
    Direct if possible, relayed if not.
  6. 6
    Secure and stream
    DTLS sets up keys. SRTP carries audio and video. Data channels carry data.

Why NAT makes this hard

Most devices sit behind a router (NAT) and have a private address like 192.168.1.5. The outside world cannot connect to it directly. STUN and ICE work out a public address and open a path. If both sides are behind strict (symmetric) NAT, a direct path often cannot be made, and TURN relays the media.

Layers of WebRTC (top to bottom)
AppYour app: video UI, chat, data
APIRTCPeerConnection, MediaStream, RTCDataChannelbrowser APIs
SecSRTP (media) ยท SCTP (data)encrypted by DTLS
NetICE ยท STUN ยท TURNfind a path
IPUDP (mostly) over IP
VisualizerSignalling: offer, answer, ICEStep through how two browsers meet through a signalling server and then connect directly.
๐Ÿ‘ฉAshabrowser
โ†”no link yet
๐Ÿ‘จRavibrowser
Step 1 of 6
Two people want a video call
Asha and Ravi are in different homes. Their browsers do not know each other's address yet. They need help to meet first.
VisualizerDirect, STUN or TURN?Choose each person's network type. Try 'Both behind strict NAT'.
Asha's network
Ravi's network
Quick test
๐Ÿ‘ฉAshaNormal home router
โ†”๐Ÿ”’ direct (after STUN found the address)
๐Ÿ‘จRaviNormal home router
Route used
STUN + direct
Example delay
40 ms
illustration only
Server cost
Low
tiny: only the address lookup
A STUN server only tells each browser its public address. After that, the call goes straight between them. This works for most home and office networks.

07How to Use It / Steps / Implementation

Building a small 1-to-1 call

  1. Build a signalling server (a WebSocket server that passes JSON between two users in a room).
  2. Both browsers call getUserMedia and add the tracks to an RTCPeerConnection.
  3. Give the connection STUN and TURN servers in iceServers. Always include TURN for production.
  4. Caller: createOffer, setLocalDescription, send it. Callee: setRemoteDescription, createAnswer, send it back.
  5. Send each ICE candidate as it appears (onicecandidate) and add the ones you receive.
  6. Show remote tracks in a <video> element (ontrack).
  7. Watch connection state and handle failures (restart ICE, or reconnect).

Calls with many people

A mesh (everyone connects to everyone) works for about 3 to 4 people. After that, upload bandwidth explodes. Larger calls use a media server:

DesignHowGood for
MeshEvery peer connects to every other peer2 to 4 people, no media server
SFU (Selective Forwarding Unit)Each person uploads once to a server, which forwards streams to the othersMeetings, webinars. The most common choice
MCU (Multipoint Control Unit)Server mixes all video into one streamWeak devices, but heavy server cost
Mesh vs SFU
Mesh: 4 people = 6 links
๐Ÿ‘ฉ
๐Ÿ‘จ
โ†•every pair
๐Ÿง‘
๐Ÿ‘ง
SFU: 4 uploads, server forwards
๐Ÿ‘ฉ
๐Ÿ‘จ
โ†•
๐Ÿ“กSFU
โ†•
๐Ÿง‘
๐Ÿ‘ง

08Key Terms to Know

TermMeaning in easy words
PeerOne of the two ends of a direct connection
SignallingPassing setup messages so peers can find each other. You build it
SDPSession Description Protocol: the text that describes media and security
Offer / AnswerThe two SDP messages used to agree on a call
ICE candidateOne possible network route to a peer
STUNTells a device its public IP and port
TURNA relay server that forwards media when direct fails
NATA router feature that hides private addresses behind one public address
DTLS / SRTPEncryption for keys, and for audio and video
Data channelA direct peer-to-peer pipe for arbitrary data
SFUA server that forwards video streams to many people

09Pros, Cons and Trade-offs

โœ… Pros
  • Very low delay for audio and video
  • Media can skip your servers, which saves bandwidth cost
  • Always encrypted
  • Built into browsers and phones
  • Data channels for games and file transfer
โš ๏ธ Cons
  • You must build signalling yourself
  • NAT and firewalls make connecting hard. TURN is often needed, and it costs bandwidth
  • Group calls need extra servers (SFU or MCU)
  • Quality changes with the network, and is hard to debug
  • Large API with many moving parts

Which real-time tool?

WebSocketsWebRTC
PathClient to server to clientPeer to peer (or via a relay)
Best forChat, signals, app eventsLive audio, video, direct data
TransportTCPMostly UDP
Server roleCarries every messageOnly helps set up (and relays if needed)
SetupEasyComplex
๐Ÿ’กThey work together
Most WebRTC apps use a WebSocket for signalling and chat messages, and WebRTC for the live media.

10Common Problems and Mistakes

Common mistakes

  1. No TURN server. It works on your Wi-Fi, and fails for users behind strict networks. Many real calls need TURN.
  2. Mesh for big meetings. Each person uploads many copies, and the call collapses. Use an SFU.
  3. Forgetting security of signalling. Anyone could join a room. Authenticate users and rooms.
  4. Public TURN credentials. Attackers use your TURN for free traffic. Use short-lived credentials.
  5. Ignoring bandwidth changes. Let the browser adapt, and provide a way to turn video off.
  6. Not handling permission denied or no camera. Show a clear message.
  7. Testing only on one network. Test on mobile data and on office Wi-Fi too.
๐Ÿ“‰Choppy video
Packet loss and low bandwidth. Lower the resolution or frame rate, and use adaptive bitrate.
๐Ÿ”‡One-way audio
A NAT or firewall blocks one direction. Check TURN and the ICE state.

11Real-World Examples

๐Ÿ“นBrowser meetings
Many video-meeting tools use WebRTC in the browser, with SFU servers for groups (general idea).
๐ŸŽ™๏ธVoice in games and apps
In-app voice chat often uses WebRTC.
๐ŸฅTelehealth
Doctor-patient video visits in a browser.
๐Ÿ“Peer-to-peer sharing
Direct file send between two browsers.
Use WebRTC when...
  • Live audio or video between people
  • You want low delay
  • You want to avoid sending media via your servers
  • Direct data between users
Use something else when...
  • Normal chat or events: WebSockets or SSE
  • One-way broadcast to millions: a streaming service with a CDN
  • Simple notifications: webhooks or push

12Related Topics to Read Next

WebSocketsโ†’

The usual choice for the signalling channel.

TCP vs UDPโ†’

WebRTC media mostly uses UDP for speed.

IP Addressโ†’

Public and private addresses explain why NAT is a problem.

Proxy vs Reverse Proxyโ†’

TURN is a kind of relay for media.

HTTP / HTTPSโ†’

Signalling and TLS-protected pages are needed for camera access.

Server-Sent Events (SSE)โ†’

Another real-time option for one-way updates.

13Interview Questions with Short Answers

14Quick Revision Cheat-Sheet

  • โœ”What: real-time audio, video and data directly between browsers, always encrypted.
  • โœ”Setup: signalling (offer, answer, ICE candidates) through your own server.
  • โœ”Network help: STUN finds the public address. TURN relays when direct fails.
  • โœ”Security: DTLS for keys, SRTP for media. It is mandatory.
  • โœ”Groups: mesh for 2 to 4. SFU for larger calls.
  • โœ”Remember: always add a TURN server for real users.
  • โœ”One line: โ€œA server introduces the two people, then they talk directly.โ€
โ† Previous
Webhooks
Next โ†’
Short Polling