Explain WebRTC vs WebSocket for Realtime.
WebRTC enables peer-to-peer ultra-low-latency audio, video, and data transfer, while WebSocket provides reliable, ordered, bidirectional client-server messaging.
When to Use
- WebRTC: video/audio calls, live streaming, multiplayer games, screensharing (<200ms latency).
- WebSocket: chat apps, collaborative docs, IoT control, stock tickers, notifications, dashboards.
Example
In a video call app, WebSocket handles signaling; WebRTC carries the actual media. In a live stock dashboard, a single WebSocket efficiently streams price updates.
Want to prepare deeper?
Explore:
- Grokking System Design Fundamentals
- Grokking the Coding Interview
- Mock Interviews with ex-FAANG engineers
Why Is It Important
Choosing the right protocol affects latency, scalability, and reliability. WebRTC reduces bandwidth with P2P, while WebSocket simplifies central server control.
Interview Tips
- Lead with: “Media → WebRTC, messaging → WebSocket.”
- Mention STUN/TURN, ICE, and signaling flows.
- Call out trade-offs like TURN server costs vs WebSocket simplicity.
Trade-offs
WebRTC
Pros: sub-150ms latency and P2P cost savings Cons: setup complexity, NAT/firewall issues, and TURN server egress cost.
WebSocket
Pros: simple API, ordered messages, and robust libraries Cons: less optimal for high-volume media.
Pitfalls
- Confusing signaling with transport.
- Assuming P2P always works.
- Overusing WebRTC DataChannel for broadcast.
- Forgetting reconnection/backpressure handling.
GET YOUR FREE
Coding Questions Catalog
$197

$78
$78