~learn.
?
← Case filescheckpoint 1 / 11 · ~18 min left
CASE-002SYSTEM DESIGN18 MIN

The Great Video Call Revolution: When WebSockets Met WebRTC and Changed Everything

Himanshu Kukrejareads
The Great Video Call Revolution: When WebSockets Met WebRTC and Changed Everything
FIG. 1 · Blueprint · CASE-002
watch it first · newThe whole case in a 11-minute animated lecture3 quiz checkpoints · ask the AI about any moment · captions, chapters and speed

Lesson · 16 min

Understanding the Problem: The Battle for Real-Time Supremacy#

Meet Emma Rodriguez, CTO of ConnectNow, a promising startup building the next generation of virtual collaboration tools. With remote work exploding post-2020, her team is racing to create a platform that combines video conferencing, screen sharing, and real-time document collaboration—all in a single web application.

At 3 AM during a crucial investor demo preparation, disaster strikes. Their video calling feature, built entirely on WebSockets, starts stuttering and failing under load. While their text chat works flawlessly, the video streams become unwatchable pixelated messes with 3-second delays. Emma realizes they've chosen the wrong tool for the job, and tomorrow's $5M funding round hangs in the balance.

The Technical Crisis:

  • Video quality: Dropping to 240p with severe pixelation
  • Latency: 2-5 second delays making conversations impossible
  • Server load: CPU hitting 95% with just 50 concurrent video calls
  • Bandwidth costs: Skyrocketing as all video routes through their servers
  • User experience: Investors losing confidence in real-time

Our Mission: Understand when to use WebSockets versus WebRTC for video communication, and why making the wrong choice can kill your business before it starts.

ExplainerThe whole story: WebSockets vs WebRTC for video

ConnectNow's video over WebSockets ran at 240p, 2 to 5 seconds behind, with CPU at 95% on 50 calls, because every frame passed through the server. WebSockets suit text over TCP; WebRTC signals once, then streams peer to peer over UDP in about 100 to 300 ms, with STUN, TURN and SFUs for hard networks and groups. The hybrid cut latency below 100 ms, CPU to 15% at 500 users and bandwidth cost tenfold.

The Architecture Showdown: Client-Server vs Peer-to-Peer#

ConnectNow's Original WebSocket Architecture:

DiagramConnectNow's original WebSocket architecture
%% title: ConnectNow's original WebSocket architecture
flowchart TD
    A@{ icon: "sd:browser", label: "User A (Browser)<br/>Video Stream & Capture" } --> WS["WebSocket Server (Relay Hub)<br/>Process & Forward All Video"]:::bad
    WS --> B@{ icon: "sd:browser", label: "User B (Browser)<br/>Video Stream Display" }
    WS -.- N["BOTTLENECK: Every video packet<br/>flows through server"]:::note
    class A,B client

The Better WebRTC Architecture:

DiagramThe better WebRTC architecture
%% title: The better WebRTC architecture
flowchart TD
    A@{ icon: "sd:browser", label: "User A (Browser)" } <==>|"Direct P2P Video Communication<br/>Video Stream (Direct)"| B@{ icon: "sd:browser", label: "User B (Browser)" }
    A -.->|"Setup Handshake"| S["Signaling Server (Setup Only)<br/>Lightweight · No video data processing"]:::service
    B -.->|"Setup Handshake"| S
    class A,B client

WebSockets: The Reliable Workhorse#

How WebSockets Work: The TCP Foundation#

WebSockets provide a persistent, full-duplex communication channel between client and server over TCP. Think of it as upgrading a regular HTTP request-response into a permanent telephone line where both sides can talk anytime.

The WebSocket Handshake Process:

DiagramThe WebSocket handshake process
%% title: The WebSocket handshake process
sequenceDiagram
    autonumber
    participant C as Client
    participant S as Server
    C->>S: HTTP request with special headers<br/>Upgrade: websocket · Connection: Upgrade<br/>Sec-WebSocket-Key: [random key]
    S-->>C: HTTP 101 Switching Protocols<br/>Upgrade: websocket · Connection: Upgrade<br/>Sec-WebSocket-Accept: [computed response]
    Note over C,S: Connection upgraded to WebSocket protocol
    Note over C,S: Both sides can now send messages freely

WebSocket Strengths: Why They Excel#

Perfect for Text-Based Real-Time Communication:

  • Guaranteed delivery: TCP ensures every message arrives in order
  • Low overhead: After handshake, minimal protocol overhead
  • Broad compatibility: Supported by all modern browsers
  • Server-centric: Easy to implement authentication, logging, and moderation

Real-World WebSocket Success Stories:

Slack's Messaging Architecture:

  • Uses WebSockets for instant message delivery
  • Handles typing indicators and read receipts
  • Sends millions of messages a day; Slack says its system delivers them across the world in about 500ms
  • Server manages all message routing and history

Discord's Communication Platform:

  • WebSockets for text chat and presence updates
  • Real-time activity status and notifications
  • Scales to millions of concurrent users
  • Central server manages all user state

WebSocket Limitations for Video#

The Video Streaming Problem:

DiagramVideo frame journey via WebSocket
%% title: Video frame journey via WebSocket
sequenceDiagram
    autonumber
    participant A as User A
    participant WS as WebSocket server
    participant B as User B
    Note over A: Captures video frame<br/>30 FPS = 33ms intervals
    A->>WS: Frame encoded and sent
    Note over WS: Receives frame and processes
    WS->>B: Forwards frame
    Note over B: Receives and displays frame
    Note over A,B: Total latency: Network RTT + Server Processing + Queuing Delays<br/>Typical result: 200-500ms delay (unacceptable for real-time)

Resource Consumption Issues:

  • Server CPU: Must process every video frame
  • Bandwidth costs: All video data flows through servers
  • Memory usage: Buffering video streams for multiple users
  • Scaling challenges: Each additional user multiplies server load

WebRTC: The Peer-to-Peer Powerhouse#

How WebRTC Works: Direct Communication Magic#

WebRTC (Web Real-Time Communication) enables direct peer-to-peer connections between browsers for audio, video, and data. It's designed specifically for real-time media streaming with ultra-low latency.

WebRTC Connection Establishment:

DiagramWebRTC connection establishment
%% title: WebRTC connection establishment
sequenceDiagram
    autonumber
    participant A as Peer A
    participant S as Signaling server
    participant B as Peer B
    Note over A,B: Signaling Phase (uses WebSocket or HTTP)
    Note over A: Creates RTCPeerConnection
    A->>S: "offer" (SDP - Session Description Protocol)
    S->>B: Offer sent to Peer B
    B->>S: "answer"
    S->>A: Answer sent back
    Note over A,B: ICE candidates exchanged for NAT traversal
    A->>B: Direct P2P connection established
    Note over A,B: Media Streaming Phase
    A->>B: Audio/video captured in browser, sent directly
    B->>A: Media streams sent directly between peers
    Note over A,B: No server involvement in media transfer<br/>Low latency (~100-300ms glass-to-glass typical)

The NAT Traversal Challenge#

Why Direct P2P is Complex: Most devices sit behind NAT (Network Address Translation) firewalls that block direct connections. WebRTC solves this through:

STUN Servers (Session Traversal Utilities for NAT):

  • Help discover public IP addresses
  • Enable direct connections when possible
  • Lightweight and cheap to operate

TURN Servers (Traversal Using Relays around NAT):

  • Relay traffic when direct connection impossible
  • Used as fallback (commonly cited as roughly 10-20% of connections, more on corporate networks)
  • More expensive but ensures connectivity
ExplainerVideo over WebSockets vs WebRTC

Over WebSockets every one of User A's 30 frames a second goes through ConnectNow's server, which processes and forwards it: 200-500 ms, and CPU at 95% with 50 calls. WebRTC exchanges an SDP offer and answer through a signaling server, uses ICE and STUN to open a direct path, then streams peer to peer over UDP at ~100-300 ms; when NAT blocks that path (~10-20% of connections) a TURN server relays the media.

WebRTC Strengths: Built for Media#

Optimized for Real-Time Media:

  • UDP-based: Prioritizes speed over guaranteed delivery
  • Built-in codecs: Efficient video/audio compression (H.264, VP8, Opus)
  • Adaptive bitrate: Automatically adjusts quality based on network conditions
  • Minimal latency: Direct P2P adds little beyond network transit (often <50ms nearby); ~100-300ms glass-to-glass including capture, encoding and jitter buffer

Real-World WebRTC Success Stories:

Google Meet's Architecture:

  • Uses WebRTC for all video/audio streams
  • Handles millions of concurrent calls
  • Automatic quality adaptation based on network conditions
  • Routes group calls through Google's own media servers (SFUs) worldwide: WebRTC doesn't mean serverless

Zoom's Hybrid Approach:

  • Browser client: historically WebAssembly codecs over WebRTC DataChannels; its web Video SDK added standard WebRTC media in late 2024
  • Custom protocols for native apps
  • Intelligent routing between P2P (two-person meetings) and server relay
  • Relies on its own global data-center network for low latency

WebRTC Limitations#

Implementation Complexity:

  • Signaling required: Need separate server for connection setup
  • NAT traversal: STUN/TURN servers required for reliability
  • Browser compatibility: Some API differences between browsers
  • Media handling: Complex codec negotiation and error handling

Scalability Challenges:

  • P2P limits: Direct connections don't scale beyond small groups
  • Server fallback: Need infrastructure for TURN relay scenarios
  • Multi-party complexity: Mesh networks become inefficient with many participants

The Great Comparison: When to Choose What#

Performance and Latency Analysis#

Metric WebSockets WebRTC Winner
Text Chat Latency 50-100ms 50-100ms (via data channels) Tie
Video Streaming Latency 200-500ms+ (seconds under packet loss) ~100-300ms typical WebRTC
Audio Quality Depends on server processing Optimized codecs WebRTC
Server Resource Usage High (processes all media) Low (signaling only) WebRTC
Bandwidth Costs High (all traffic via server) Low (direct P2P) WebRTC
Implementation Complexity Low High WebSockets

Use Case Decision Matrix#

Choose WebSockets When:#

Real-Time Text Communication:

  • Chat applications: Slack, Discord, WhatsApp Web
  • Collaborative editing: Figma, Notion
  • Live updates: Stock prices, sports scores, notifications
  • Gaming: Turn-based games, chat systems

Why WebSockets Win Here:

  • Guaranteed message delivery via TCP
  • Server can enforce business logic and moderation
  • Easy to implement authentication and logging
  • Messages can be persisted and replayed

Example Architecture - Live Trading Platform:

DiagramLive trading platform · real-time stock price updates
%% title: Live trading platform · real-time stock price updates
flowchart TD
    T["Trading Server"]:::core -->|"Price Feed"| B1@{ icon: "sd:browser", label: "Browser 1" }
    T -->|"Price Feed"| B2@{ icon: "sd:browser", label: "Browser 2" }
    T -->|"Price Feed"| BN@{ icon: "sd:browser", label: "Browser N" }
    class B1,B2,BN client

WebSocket Use Case: Real-time stock price updates

Advantages:

  • ✅ Guaranteed delivery of price updates
  • ✅ Server can enforce rate limiting
  • ✅ Easy to implement price history
  • ✅ Can handle millions of concurrent price feeds
Choose WebRTC When:#

Real-Time Media Communication:

  • Video conferencing: Zoom, Google Meet, Teams
  • Live streaming: interactive, low-latency streams (big platforms like Twitch and YouTube Live mainly deliver via HTTP streaming such as HLS/DASH)
  • Screen sharing: Remote desktop, presentations
  • Gaming: Real-time multiplayer with voice chat
  • IoT streaming: Security cameras, baby monitors

Why WebRTC Wins Here:

  • Ultra-low latency for media streams
  • Built-in media codecs and quality adaptation
  • Direct P2P reduces server costs
  • Optimized for audio/video processing

Example Architecture - Video Conferencing:

DiagramVideo conferencing · 4-person call
%% title: Video conferencing · 4-person call
flowchart TD
    A@{ icon: "sd:user", label: "User A" } <--> B@{ icon: "sd:user", label: "User B" }
    A <--> D@{ icon: "sd:user", label: "User D" }
    B <--> C@{ icon: "sd:user", label: "User C" }
    D <--> C
    A <--> C
    B <--> D
    S["Signaling Server (WebSocket/HTTP)<br/>manages setup only"]:::service
    C ~~~ S
    class A,B,C,D client

WebRTC Use Case: 4-person video call

Advantages:

  • ✅ ~100-300ms video latency
  • ✅ Direct P2P connections
  • ✅ Automatic quality adaptation
  • ✅ Built-in security and encryption

The Hybrid Architecture: Best of Both Worlds#

ConnectNow's Winning Solution#

After the investor demo disaster, Emma's team implemented a hybrid architecture that uses both technologies strategically:

DiagramConnectNow's hybrid architecture
%% title: ConnectNow's hybrid architecture
flowchart TD
    subgraph SIG["Signaling Layer (WebSocket Server)"]
        direction TB
        S1["Handles user authentication<br/>and presence"]:::edge
        S2["Negotiates WebRTC connections"]:::edge
        S3["Manages text chat, notifications,<br/>meetings, and recording controls"]:::edge
        S1 ~~~ S3
    end
    subgraph MED["Media Layer (WebRTC Peer-to-Peer Connections)"]
        direction TB
        M1["Streams video and audio<br/>directly between participants"]:::core
        M2["Supports screen sharing, file transfers,<br/>and real-time collaboration"]:::core
        M3["Optimizes quality based on<br/>network conditions"]:::core
        M1 ~~~ M3
    end
    SIG --> MED

Implementation Strategy#

Phase 1: WebSocket Foundation

text24 lines
// WebSocket handles signaling and chat
const signalingSocket = new WebSocket('wss://connectnow.com/signaling');

signalingSocket.onmessage = (event) => {
    const message = JSON.parse(event.data);
    
    switch(message.type) {
        case 'offer':
            handleWebRTCOffer(message.offer);
            break;
        case 'answer':
            handleWebRTCAnswer(message.answer);
            break;
        case 'ice-candidate':
            handleICECandidate(message.candidate);
            break;
        case 'chat-message':
            displayChatMessage(message);
            break;
        case 'user-joined':
            updateUserList(message.users);
            break;
    }
};

Phase 2: WebRTC Media Streams

text25 lines
// WebRTC handles direct video/audio
const peerConnection = new RTCPeerConnection({
    iceServers: [
        { urls: 'stun:stun.connectnow.com:3478' },
        { 
            urls: 'turn:turn.connectnow.com:3478',
            username: 'connectnow-user',
            credential: 'secure-token'
        }
    ]
});

// Get user media and add to peer connection
navigator.mediaDevices.getUserMedia({ video: true, audio: true })
    .then(stream => {
        localVideo.srcObject = stream;
        stream.getTracks().forEach(track => {
            peerConnection.addTrack(track, stream);
        });
    });

// Handle incoming remote streams
peerConnection.ontrack = (event) => {
    remoteVideo.srcObject = event.streams[0];
};

Results: From Disaster to Success#

Before (WebSocket-only video):

  • Video latency: 2-5 seconds
  • Server CPU: 95% with 50 users
  • Bandwidth costs: $0.50 per user per hour
  • Video quality: 240p maximum
  • Investor confidence: Low

After (Hybrid architecture):

  • Video latency: <100ms
  • Server CPU: 15% with 500 users
  • Bandwidth costs: $0.05 per user per hour
  • Video quality: 1080p adaptive
  • Investor confidence: Series A funded!

Real-World War Stories: Learning from Failures and Successes#

The Telemedicine Disaster: When Latency Kills#

MedConnect's Crisis (a composite example): A telemedicine startup built their platform using WebSockets for video calls between doctors and patients. During the COVID-19 surge:

The Problem:

  • Video delays of 3-5 seconds made medical consultations unusable
  • Doctors couldn't properly examine patients due to lag
  • Emergency consultations became dangerous due to communication delays
  • Patient satisfaction scores plummeted to 2.1/5 stars

The Solution:

  • Migrated to WebRTC for all video/audio communication
  • Kept WebSockets for appointment scheduling and text notes
  • Implemented adaptive bitrate for poor network conditions
  • Added TURN servers for patients behind restrictive firewalls

The Results:

  • Video latency reduced to <200ms
  • Patient satisfaction improved to 4.7/5 stars
  • Able to handle 10x more concurrent consultations
  • Became the preferred platform for major hospital networks

The Gaming Platform Success: Discord's Hybrid Mastery#

Discord's Architecture Genius: Discord perfectly demonstrates the hybrid approach:

WebSockets for:

  • Text chat in channels (millions of messages daily)
  • User presence and status updates
  • Server management and permissions
  • Bot integrations and commands

WebRTC for:

  • Voice channels with crystal-clear audio
  • Video calls and screen sharing
  • Low-latency game streaming (Go Live)
  • Media relayed through Discord's own voice servers (SFUs), not peer-to-peer

The Results:

  • Supports 150+ million monthly active users (2021 figure)
  • Millions of concurrent voice users on low-latency voice
  • Text messages delivered in near real time
  • Scales globally on its own fleet of voice servers

The Emergency Response Revolution: Carbyne's Life-Saving Tech#

Carbyne's 911 System: Emergency services require ultra-low latency for life-or-death situations:

The Challenge:

  • 911 calls need instant video from accident scenes
  • Every second of delay could mean the difference between life and death
  • Must work reliably under network stress and poor conditions
  • Need to distribute video to multiple emergency responders simultaneously

The Solution (the typical design for this kind of system; Carbyne's internals are simplified here):

  • WebRTC for initial ultra-low latency video capture (<500ms)
  • Immediate streaming from caller's phone to 911 operator
  • WebSocket signaling for call setup and metadata
  • Hybrid relay system for distribution to paramedics and hospitals

The Impact:

  • Callers stream live video from a browser link, with no app install
  • Enables remote medical guidance during critical situations
  • Dispatchers see the scene instead of relying on a verbal description

Advanced Patterns and Optimization Strategies#

Scaling WebRTC: Beyond Simple P2P#

The Mesh Network Problem:

text12 lines
4-Person Call Bandwidth Requirements:
- Each person sends video to 3 others
- Each person receives video from 3 others
- Total: 6 video streams per person
- With 1080p: ~18 Mbps per participant

10-Person Call:
- Each person: 18 video streams
- Total: ~54 Mbps per participant (~27 Mbps of it upload)
- Plus encoding 9 separate outgoing streams on one device
- Many home connections (especially cable) can't sustain that upload
- Result: Impractical!

Solution 1: Selective Forwarding Unit (SFU)

DiagramSFU architecture
%% title: SFU architecture
flowchart TD
    A@{ icon: "sd:user", label: "User A" } <--> SFU["SFU (Server)<br/>Receives all feeds<br/>Forwards without encoding"]:::core
    B@{ icon: "sd:user", label: "User B" } <--> SFU
    SFU <--> C@{ icon: "sd:user", label: "User C" }
    SFU <--> D@{ icon: "sd:user", label: "User D" }
    class A,B,C,D client

Advantages:

  • ✅ Each user uploads once, receives multiple streams
  • ✅ Server doesn't encode/decode (lower CPU)
  • ✅ Scales to 50+ participants
  • ✅ Lower bandwidth per participant

Solution 2: Multipoint Control Unit (MCU)

DiagramMCU architecture
%% title: MCU architecture
flowchart TD
    A@{ icon: "sd:user", label: "User A" } <--> MCU["MCU (Server)<br/>Mixes all video streams<br/>Produces a single combined stream"]:::core
    B@{ icon: "sd:user", label: "User B" } <--> MCU
    MCU <--> C@{ icon: "sd:user", label: "User C" }
    MCU <--> D@{ icon: "sd:user", label: "User D" }
    class A,B,C,D client

Advantages:

  • ✅ Ultra-low bandwidth (one stream per user)
  • ✅ Consistent quality for all participants
  • ✅ Can add effects and branding
  • ❌ High server CPU usage
  • ❌ Introduces additional latency

Network Optimization Techniques#

Adaptive Bitrate Streaming:

text34 lines
// Monitor connection quality and adapt
const connection = peerConnection.getStats()
    .then(stats => {
        stats.forEach(report => {
            // remote-inbound-rtp = the peer's report on OUR outgoing video
            // (inbound-rtp would describe what we receive, not what we send)
            if (report.type === 'remote-inbound-rtp' && report.kind === 'video') {
                const lossRate = report.fractionLost;
                
                if (lossRate > 0.05) {
                    // High packet loss - reduce quality
                    adjustVideoQuality('low');
                } else if (lossRate < 0.01) {
                    // Good connection - increase quality
                    adjustVideoQuality('high');
                }
            }
        });
    });

function adjustVideoQuality(quality) {
    const sender = peerConnection.getSenders()
        .find(s => s.track && s.track.kind === 'video');
    
    const params = sender.getParameters();
    
    if (quality === 'low') {
        params.encodings[0].maxBitrate = 300000; // 300 kbps
    } else if (quality === 'high') {
        params.encodings[0].maxBitrate = 2000000; // 2 Mbps
    }
    
    sender.setParameters(params);
}

Security and Privacy Considerations#

WebRTC Security Advantages:

  • Mandatory encryption: All media streams encrypted by default using DTLS-SRTP (end-to-end in P2P; an SFU decrypts unless you add E2EE via Insertable Streams)
  • Identity verification: Cryptographic fingerprints prevent man-in-the-middle attacks
  • Secure by design: No media data touches servers in P2P mode

WebSocket Security Requirements:

  • WSS (Secure WebSocket): Must use TLS encryption for production
  • Authentication: Server-side validation of all messages
  • Rate limiting: Prevent spam and DoS attacks
  • Input validation: Sanitize all incoming data

Hybrid Security Architecture:

text22 lines
// Secure WebSocket connection for signaling
// (browsers can't set custom headers on a WebSocket: authenticate in the first message)
const signalingSocket = new WebSocket('wss://api.connectnow.com/signaling');
signalingSocket.onopen = () => signalingSocket.send(JSON.stringify({
    type: 'auth',
    token: jwtToken,
    clientVersion: '2.1.0'
}));

// WebRTC with secure TURN authentication
const rtcConfig = {
    iceServers: [
        { urls: 'stun:stun.connectnow.com:3478' },
        {
            urls: 'turn:turn.connectnow.com:3478',
            username: generateTurnUsername(),
            credential: generateTurnCredential(), // Time-limited token
            credentialType: 'password'
        }
    ],
    iceCandidatePoolSize: 10
};

Implementation Guidelines and Best Practices#

The Decision Tree: Choosing Your Technology Stack#

FIG 1 · Real-Time Communication Requirements
Real-Time Communication Requirements:
├── Primary Use Case?
│   ├── Text/Data Communication
│   │   ├── Chat applications → WebSockets
│   │   ├── Live updates (stocks, sports) → WebSockets
│   │   ├── Collaborative editing → WebSockets
│   │   └── Notifications → WebSockets
│   │
│   └── Media Communication  
│       ├── Video conferencing → WebRTC
│       ├── Live streaming (interactive) → WebRTC
│       ├── Screen sharing → WebRTC
│       └── File transfer → WebRTC DataChannels
│
├── Latency Requirements?
│   ├── Conversational (<~300ms) → WebRTC
│   ├── <500ms acceptable → Either
│   └── >500ms acceptable → WebSockets
│
├── Scaling Requirements?
│   ├── ≤4 participants → WebRTC P2P mesh
│   ├── 5-100 participants → WebRTC + SFU
│   ├── 100+ participants → WebRTC + cascaded SFUs (MCU if clients can decode only one stream)
│   └── Broadcast (1-to-many) → WebRTC/RTMP ingest + HLS via CDN
│
└── Infrastructure Budget?
    ├── Low budget → WebRTC (P2P saves bandwidth)
    ├── Medium budget → Hybrid approach
    └── High budget → Custom optimized solution

Development and Testing Strategy#

Phase 1: Start Simple

text10 lines
// Begin with basic WebSocket chat
const basicSocket = new WebSocket('wss://yourapp.com/chat');
basicSocket.onmessage = (event) => {
    const message = JSON.parse(event.data);
    displayMessage(message.text, message.user);
};

// Test with multiple browser tabs locally
// Validate message delivery and ordering
// Implement reconnection logic

Phase 2: Add WebRTC Gradually

text10 lines
// Start with audio-only WebRTC
navigator.mediaDevices.getUserMedia({ audio: true })
    .then(stream => {
        // Add audio track to peer connection
        // Test on same network first
        // Then test across different networks
    });

// Expand to video once audio is stable
// Add screen sharing as final feature

Phase 3: Optimize and Scale

text4 lines
// Implement connection quality monitoring
// Add adaptive bitrate streaming
// Set up TURN servers for production
// Load test with realistic user scenarios
✗ Pitfall

Common Pitfalls and How to Avoid Them#

WebSocket Pitfall #1: Using for Media Streaming

text13 lines
// ❌ DON'T: Stream video through WebSocket
socket.send(JSON.stringify({
    type: 'video-frame',
    data: videoFrame, // Huge payload!
    timestamp: Date.now()
}));

// ✅ DO: Use WebSocket for signaling only
socket.send(JSON.stringify({
    type: 'webrtc-offer',
    sdp: offer.sdp,
    userId: currentUser.id
}));

WebRTC Pitfall #1: Forgetting TURN Servers

text16 lines
// ❌ DON'T: STUN servers only (fails for a sizable minority, often cited as 10-20% of connections)
const rtcConfig = {
    iceServers: [{ urls: 'stun:stun.l.google.com:19302' }]
};

// ✅ DO: Include TURN servers for reliability
const rtcConfig = {
    iceServers: [
        { urls: 'stun:stun.l.google.com:19302' },
        { 
            urls: 'turn:your-turn-server.com:3478',
            username: 'user',
            credential: 'pass'
        }
    ]
};

WebRTC Pitfall #2: Not Handling Connection States

text19 lines
// ❌ DON'T: Ignore connection state changes
peerConnection.oniceconnectionstatechange = null;

// ✅ DO: Monitor and handle all states
peerConnection.oniceconnectionstatechange = () => {
    switch(peerConnection.iceConnectionState) {
        case 'connected':
            showConnectionStatus('Connected');
            break;
        case 'disconnected':
            showConnectionStatus('Reconnecting...');
            break;
        case 'failed':
            showConnectionStatus('Connection failed');
            // Attempt reconnection
            restartICE();
            break;
    }
};

The Future of Real-Time Communication#

What's next for WebRTC:

  • AV1 codec support (Chrome has shipped an AV1 encoder for WebRTC since version 90): often cited as up to ~50% smaller than H.264 at similar quality
  • Machine learning integration: AI-powered noise cancellation and video enhancement
  • 5G optimization: Ultra-low latency for mobile applications
  • IoT integration: Direct browser-to-device communication

WebTransport: The Next Evolution

text12 lines
// WebTransport (HTTP/3) combines best of both worlds
const transport = new WebTransport('https://api.example.com/webtransport');
await transport.ready;
const encoder = new TextEncoder();  // streams and datagrams carry bytes, not strings

// Reliable streams (like WebSocket)
const reliableStream = await transport.createBidirectionalStream();
reliableStream.writable.getWriter().write(encoder.encode('reliable message'));

// Unreliable datagrams (like WebRTC)
const datagramWriter = transport.datagrams.writable.getWriter();
datagramWriter.write(encoder.encode('low-latency data'));

The Hybrid Architecture Revolution (the author's predictions, not measured data):

  • Hybrid by default: most new real-time apps pairing WebSocket (or WebTransport) signaling with WebRTC media
  • Edge media servers: SFUs pushed closer to users to cut the network part of latency
  • AI-assisted tooling: automatic technology selection based on use case

Summary: The Technology Choice That Changes Everything#

The battle between WebSockets and WebRTC isn't about one technology being superior—it's about choosing the right tool for the specific job. Emma's ConnectNow startup learned this lesson the hard way, but their recovery story demonstrates the power of understanding each technology's strengths.

The Golden Rules:

  1. Use WebSockets for reliable data communication: Text chat, notifications, collaborative editing, and any scenario where message delivery guarantees matter more than ultra-low latency.

  2. Use WebRTC for real-time media: Video calls, interactive live streaming, screen sharing, and any application where conversational latency (a few hundred milliseconds at most) is critical for user experience.

  3. Embrace hybrid architectures: The most successful platforms use WebSockets for signaling and control, WebRTC for media streams, creating the best of both worlds.

  4. Plan for scale from day one: Whether it's WebSocket connection limits or WebRTC mesh network bandwidth explosion, understand the scaling challenges before they hit production.

The Business Impact:

Choosing the wrong technology can kill promising startups:

  • WebSocket-only video: High latency, poor quality, expensive servers
  • WebRTC-only text: Over-engineered, complex implementation, unnecessary overhead
  • Hybrid approach: Optimal performance, cost-effective scaling, best user experience

The Technical Truth:

  • WebSockets excel at reliable, server-mediated communication where TCP's guarantees and central control are valuable
  • WebRTC dominates real-time media streaming where UDP's speed and direct P2P connections minimize latency
  • The future belongs to hybrid architectures that leverage each technology's strengths

The Ultimate Lesson:

Technology choices aren't just technical decisions—they're business decisions. The difference between ~150ms and multi-second video latency isn't just numbers on a monitoring dashboard; it's the difference between a $5M funding round and a failed startup. Understanding when to use WebSockets versus WebRTC isn't just about knowing protocols; it's about understanding user experience, scaling economics, and competitive advantage.

Emma's team learned that the most elegant solution often isn't choosing one technology over another—it's orchestrating multiple technologies to create something greater than the sum of its parts. In the world of real-time communication, the winners aren't those who pick the "best" technology, but those who pick the right combination of technologies for their specific challenge.

The Final Truth: In real-time communication, latency isn't just a technical metric—it's the difference between human connection and digital frustration. Choose WebSockets when reliability matters most, choose WebRTC when latency matters most, and choose hybrid architectures when you want to win in the market.

End of file · CASE-002

Read it through to close the case.

0/11 sections read. Each one counts after a few minutes of actual reading in it; skipping ahead doesn't. The case closes itself once every section is read.

All case files →
> debrief --interactiveCASE-002 · +25 XP at 8/11
End-of-file activity · topology picker

Pick the pipe

Emma's demo died because video went through a WebSocket server. Route six ConnectNow features to the right pipe, size a WebRTC call, and fix the firewall case.

  • ~2 MIN
  • 4 STEPS · 11 PTS
  • SOLVE AT 8/11
  • +25 XP

this case in shorts · 2

play them all →

One idea from this case in about a minute each, made for your phone.

WebSocketsWebRTCVideo StreamingReal-time CommunicationWeb TechnologiesSystem Architecture

Discussion

Add to the post-mortem
loading the record…