DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
  1. Home
  2. Articles
  3. Frontend Engineering
  4. Understanding WebSockets: Real-Time Communication in Production
XLinkedInReddit
MediumFrontend Engineering

Understanding WebSockets: Real-Time Communication in Production

D
DevPrep Team
February 10, 2026·1 min read·0
Table of Contents
  • HTTP vs WebSocket
  • The Handshake
  • Production Gotchas
  • 1. Reconnection Logic
  • 2. Heartbeat/Ping-Pong
  • 3. Message Ordering
  • When NOT to Use WebSockets
  • Scaling WebSockets

At Google, we use WebSockets extensively for real-time features like collaborative editing and live notifications. Here's what you need to know.

HTTP vs WebSocket

HTTP is request-response. WebSocket is full-duplex — both client and server can send messages independently after the initial handshake.

// Client-side WebSocket
const ws = new WebSocket("wss://api.example.com/ws");
ws.onopen = () => ws.send(JSON.stringify({ type: "subscribe", channel: "updates" }));
ws.onmessage = (event) => {
  const data = JSON.parse(event.data);
  handleUpdate(data);
};
ws.onclose = (event) => {
  if (!event.wasClean) reconnectWithBackoff();
};

The Handshake

WebSocket starts as an HTTP upgrade request. The server responds with 101 Switching Protocols. After that, the connection is persistent and bidirectional.

Production Gotchas

1. Reconnection Logic

Connections drop. Always implement exponential backoff:

function reconnectWithBackoff(attempt = 0) {
  const delay = Math.min(1000 * Math.pow(2, attempt), 30000);
  setTimeout(() => connect(attempt + 1), delay);
}

2. Heartbeat/Ping-Pong

Load balancers and proxies kill idle connections. Send periodic pings to keep the connection alive. At Google, we use 30-second intervals.

3. Message Ordering

WebSocket guarantees order within a single connection, but if you reconnect, you might miss messages. Use sequence numbers or timestamps to detect gaps.

When NOT to Use WebSockets

  • Server-Sent Events (SSE): If you only need server→client updates, SSE is simpler and works over HTTP/2
  • Long Polling: For low-frequency updates where WebSocket overhead isn't justified
  • HTTP/2 Server Push: For pushing resources, not data

Scaling WebSockets

Each WebSocket connection holds a TCP socket. At scale, you need sticky sessions or a pub/sub layer (Redis, Kafka) to broadcast across multiple server instances.

Related Articles

MediumFrontend Engineering

System Design #12: Design a Multi-Step Form Wizard

7 min read
MediumFrontend Engineering

Mastering Senior-Level JavaScript Interview Concepts

2 min read
MediumFrontend Engineering

System Design #9: Design a Collaborative Text Editor

9 min read

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Table of Contents

  • HTTP vs WebSocket
  • The Handshake
  • Production Gotchas
  • 1. Reconnection Logic
  • 2. Heartbeat/Ping-Pong
  • 3. Message Ordering
  • When NOT to Use WebSockets
  • Scaling WebSockets

Series

View all Frontend Engineering articles →

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.