Back
Web Fundamentals

WebRTC: Real-Time Communication in the Browser

Web Fundamentals
Build & Deployment: Monorepo, CI/CD, Strategies & Release SafetyState Management: Choosing the Right SolutionRedux: Predictable State Container (RTK + RTK Query)React Query (TanStack Query): Server State CachingData Fetching Patterns: REST, GraphQL, tRPC & Real-timeGraphQL Fundamentals for Frontend: Shape, Caching, and TradeoffsgRPC-Web Fundamentals: Browser Constraints and Proxy ModelCaching Strategies: Client, Server & EdgeData Normalization: Organizing State for PerformanceAPI Design Best Practices: Pagination, Errors, Versioning & Type SafetyAPI Versioning Strategies for Frontend CompatibilityPagination: Offset vs Cursor-BasedRate Limiting & API Resilience: Retries, Backoff, Jitter, IdempotencyHow Frontend Developers Can Handle Millions of API Requests Without Crashing EverythingBrowser Storage: Cookies, SessionStorage, LocalStorage, IndexedDBReal-time Communication: WebSockets, SSE & PollingWebRTC: Real-Time Communication in the BrowserCore Web Vitals: LCP, INP & CLSPerformance Optimization Trade-offsCritical Resource Prioritization: Optimize Loading OrderCode Splitting: Optimize Bundle Size with Dynamic ImportsTree Shaking: Eliminate Dead Code from Your BundleLazy Loading: Load Resources On-DemandResource Hints: Preload, Prefetch & PreconnectText Compression: Gzip and BrotliImage & Video Optimization: Modern Formats & TechniquesAdaptive Loading: Optimize for Device & NetworkList Virtualization: Render Large Lists EfficientlyWeb Workers vs Main Thread: Offloading Heavy WorkMemory Leaks in Frontend Apps: Detection & PreventionManaging Third-Party Scripts: Optimization StrategiesHow CDNs Work: Edge Delivery, Caching & PerformanceHTTP Caching Deep Dive: Cache-Control, ETag & RevalidationService Workers & Offline Strategy: Cache First, Network First & Update LifecyclePWA Fundamentals: Manifest, Installability & Offline UXCritical Rendering PathScript Loading: async vs deferEvent Loop: Understanding JavaScript Execution ModelJavaScript Module Systems: CJS vs ESM vs UMDDynamic Module Loading: import() FunctionImport on Interaction: Load When User InteractsImport on Visibility: Lazy Loading with IntersectionObserverBrowser Rendering Pipeline & Layout ThrashingRendering Strategies: CSR vs SSR vs SSG vs ISRStreaming SSR: Progressive HTML StreamingIslands Architecture: Independent Component HydrationReact Server Components: Zero-JS Server RenderingFramework Reactivity: React, Vue, Svelte & SolidHTTP/1.1 vs HTTP/2 vs HTTP/3 (QUIC) for Frontend PerformanceDNS Resolution: Path, TTL, Caching & Frontend ImpactCross-Site Scripting (XSS) AttacksCross-Site Request Forgery (CSRF) AttacksCORS Explained: Cross-Origin Resource SharingCORS Preflight in Practice: Credentials, Simple Requests & MisconfigurationsContent Security Policy (CSP)Why is HTTPS Secure? Understanding TLS/SSLAuthorization Best PracticesCookie Security & Session Hardening: SameSite, HttpOnly, Secure
hardFrontend Architecture

WebRTC: Real-Time Communication in the Browser

TL;DRWebRTC enables direct peer-to-peer audio, video, and data. Requires signaling for discovery, STUN/TURN for NAT traversal, and careful connection management.
Very High Signal
Google
Meta
Netflix
Agoda
Apple
30-Second Answerstart every interview with this

WebRTC allows browsers to establish direct peer-to-peer connections for audio, video, and arbitrary data without plugins. It consists of media capture, signaling (offer/answer + ICE candidates), and RTCPeerConnection. Understanding STUN/TURN, reconnection, and scaling is essential for production use.

The two phones need to find each other (signaling), discover how to connect through networks/firewalls (STUN/TURN), and then talk directly (peer connection). The signaling server is like a mutual friend who introduces them but doesn’t stay on the call.

Media Capture (getUserMedia)
Signaling (Offer/Answer + ICE)
STUN/TURN NAT Traversal
Peer Connection Established
Media + Data Channels Flow

1WebRTC Core Architecture

RTCPeerConnection manages the connection. Media streams come from getUserMedia() or getDisplayMedia(). Signaling (not part of the spec) exchanges SDP offers/answers and ICE candidates.

2Signaling: Offer/Answer & ICE Exchange

Peers exchange connection metadata via a signaling server (WebSocket/HTTP). ICE candidates handle network traversal.

3Media Streams & Device Control

Capture camera/mic with constraints. Add tracks to peer connection. Handle track events, muting, and device switching.

4Data Channels

Bidirectional peer-to-peer data transfer for chat, file sharing, or game state. Supports reliable and unreliable modes.

5STUN, TURN & NAT Traversal

STUN discovers public IPs. TURN relays when direct connection fails. Production apps need both.

PropertyWebRTC (P2P)SFU/MCU
LatencyVery LowLow
ScalingLimited (small groups)Excellent (100+ users)
Best For1:1 or small group callsLarge meetings
BandwidthEfficient (direct)Higher (server relay)

WebRTC (P2P)

Latency

Very Low

Scaling

Limited (small groups)

Best For

1:1 or small group calls

Bandwidth

Efficient (direct)

SFU/MCU

Latency

Low

Scaling

Excellent (100+ users)

Best For

Large meetings

Bandwidth

Higher (server relay)

Common questions

  • ›“Explain the WebRTC connection flow.”
  • ›“What is the role of signaling in WebRTC?”
  • ›“When do you need TURN servers?”
  • ›“How do you handle reconnection in WebRTC?”

What interviewers look for

  • End-to-end understanding of signaling + ICE + media flow
  • Knowledge of STUN/TURN and NAT traversal
  • Production awareness (reconnection, scaling, cleanup)
  • Security and privacy considerations

Short answer (60 sec)

WebRTC enables direct peer-to-peer audio/video/data. Requires signaling for discovery, STUN for NAT traversal, and TURN as fallback. Use RTCPeerConnection, getUserMedia, and data channels.

Detailed answer (senior level)

The flow is: capture media → create offer/answer via signaling → exchange ICE candidates → establish RTCPeerConnection → add tracks and data channels. STUN helps discover public IPs; TURN relays when direct fails. Production requires reconnection logic, resource cleanup, and careful scaling strategy.

  • Forgetting to set local/remote descriptions before sending
  • Not handling ICE candidates properly
  • No reconnection or connection state monitoring
  • Leaking media tracks (not calling stop())
  • Using WebRTC for large group calls without SFU
Key Takeaways
  • ✓WebRTC = peer-to-peer media + data in the browser
  • ✓Signaling is required but not part of the spec
  • ✓STUN for discovery, TURN for relay fallback
  • ✓Always clean up tracks and close connections
  • ✓Implement robust reconnection logic
  • ✓Use data channels for file sharing and custom data
  • ✓For large meetings, combine with SFU architecture
Previous TopicReal-time Communication: WebSockets, SSE & PollingNext Topic Core Web Vitals: LCP, INP & CLS

On this page