WebSockets & Server-Sent Events

Learn real-time web communication: polling, Server-Sent Events, the WebSocket protocol, scaling with pub/sub, security and testing.

Learn WebSockets & Server-Sent Events free, in English or Hindi, with lessons, shorts and interview questions.

Start course → · Interview questions

32 lessons · 10 modules · Free

What you'll learn

  • Choose between polling, Server-Sent Events and WebSockets for a feature and justify the trade-offs.
  • Write and parse the text/event-stream format, and build resumable SSE endpoints with Last-Event-ID.
  • Explain the WebSocket handshake, frame format, masking, ping/pong and close codes.
  • Build WebSocket servers and clients in Node.js and Python with message envelopes, rooms, heartbeats and reconnection.
  • Scale real-time systems across servers with pub/sub, handle backpressure and deploy without reconnect storms.
  • Secure real-time endpoints with authentication at upgrade, Origin checks, validation and rate limits, and debug and test them.

Syllabus

Real-time on the Web: the Mental Model

  1. Why Plain HTTP Is Not Enough
  2. Short Polling and Long Polling
  3. Choosing a Transport

Server-Sent Events Foundations

  1. The text/event-stream Format
  2. The EventSource API in the Browser
  3. Writing an SSE Endpoint

SSE in Production

  1. Reconnection and Last-Event-ID
  2. Proxies, Buffering and Connection Limits
  3. Streaming with fetch: LLM-style Responses

The WebSocket Protocol

  1. The Opening Handshake
  2. Frames, Opcodes and Masking
  3. Control Frames and the Closing Handshake

WebSocket Clients and Servers

  1. The Browser WebSocket API
  2. A WebSocket Server in Node.js with ws
  3. WebSockets in Python with asyncio

Designing Real-time Features

  1. Designing Your Message Protocol
  2. Rooms and Broadcasting
  3. Heartbeats and Dead Connections
  4. Reconnecting with Backoff and Resume

Scaling Real-time Systems

  1. Long-lived Connections and Load Balancers
  2. Fan-out Across Servers with Pub/Sub
  3. Backpressure and Slow Consumers
  4. Deploys, Restarts and Graceful Shutdown

Security for Real-time Endpoints

  1. Authenticating WebSocket Connections
  2. Origin Checks and Cross-Site WebSocket Hijacking
  3. Validation, Size Limits and Rate Limits

Libraries, Debugging and Testing

  1. Socket.IO and Higher-level Libraries
  2. Debugging with DevTools, curl and websocat
  3. Testing Real-time Code

Revision and Interview Prep

  1. Cheat Sheet
  2. Common Interview and Exam Questions
  3. Mixed Self-check

Related courses