DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
← From Beginner to Expert in Fundamentals of Frontend
  • How the Web Works
  • Browsers: The Engines
  • Networking & Latency
  • HTTPS & TLS Handshake
  • CDNs & Edge Computing
  • WebSockets & WebRTC
  • RESTful Architecture
  • GraphQL Architecture
  • CORS (Cross-Origin Resource Sharing)
  • Load Balancing
  • Critical Rendering Path Deep Dive
  • DOM vs. CSSOM Construction
  • The Rendering Engine: Layout, Paint, Compositing
  • JavaScript Engine: V8 Internals
  • The Event Loop: Macro vs. Micro Tasks
  • Garbage Collection
  • Browser Storage Architecture
  • Service Workers & PWAs
  • Web Workers: Multi-Threading
← From Beginner to Expert in Fundamentals of Frontend

WebSockets & WebRTC

22 min read
WebSocketWebRTCReal-timeBi-directionalSignalingSTUN/TURN

Real-Time Communication on the Web

HTTP is a request-response protocol — the client always initiates. But what about live chat, multiplayer games, or video calls? That's where WebSockets and WebRTC come in.

WebSockets

WebSockets provide a persistent, bi-directional connection between client and server over a single TCP connection.

How It Works:

  1. HTTP Upgrade: The connection starts as a normal HTTP request with an Upgrade: websocket header.
  2. Handshake: Server responds with 101 Switching Protocols.
  3. Full-Duplex: Both sides can now send messages at any time without HTTP overhead.

When to Use WebSockets:

  • Chat applications
  • Live dashboards & stock tickers
  • Collaborative editing (Google Docs-style)
  • Multiplayer games
  • Real-time notifications

WebRTC (Web Real-Time Communication)

WebRTC enables peer-to-peer audio, video, and data communication directly between browsers — no server relay needed for the media stream itself.

The Signaling Process:

  1. Offer/Answer: Peers exchange SDP (Session Description Protocol) messages via a signaling server.
  2. ICE Candidates: STUN servers help peers discover their public IP. If direct connection fails, TURN servers relay traffic.
  3. P2P Connection: Once established, media flows directly between browsers.

WebSocket vs. SSE vs. Polling

MethodDirectionConnectionBest For
Long PollingServer → ClientNew connection each timeLegacy compatibility
SSE (Server-Sent Events)Server → Client onlyPersistentLive feeds, notifications
WebSocketBi-directionalPersistentChat, games, collaboration
WebRTCPeer-to-PeerDirectVideo/audio calls

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.