🔗

WebSocket Guide

Verified

by Community

Creates WebSocket server and client implementations with proper connection management, heartbeat mechanisms, reconnection logic, message protocols, room/channel patterns, and scaling strategies.

websocketreal-timecommunicationbackendfrontend

WebSocket Guide

Implements real-time bidirectional communication using WebSocket protocol for web applications. Covers server setup (ws, Socket.IO, native), client connection management, heartbeat/ping-pong mechanisms, automatic reconnection with exponential backoff, message serialization protocols, room and channel patterns, authentication, and horizontal scaling with Redis pub/sub adapter.

Usage

Describe your real-time communication needs: what events need to flow between server and clients, how many concurrent connections you expect, and your server technology (Node.js, Python, Go). Specify whether you need rooms/channels, authentication, and multi-server scaling. The skill provides complete server and client implementations.

Examples

  • "Implement a WebSocket server in Node.js for a live chat app with rooms and typing indicators"
  • "Create a reconnecting WebSocket client in React with exponential backoff and offline message queuing"
  • "Build a real-time notification system that pushes updates to specific authenticated users"
  • "Design a collaborative editing WebSocket protocol with operational transform conflict resolution"

Guidelines

  • Implement ping/pong heartbeats every 30 seconds to detect dead connections and clean up server resources
  • Add automatic reconnection with exponential backoff (1s, 2s, 4s, 8s, max 30s) and jitter on the client
  • Authenticate WebSocket connections during the upgrade handshake, not after connection is established
  • Use a structured message format with type/event field, payload, and correlation ID for request-response patterns
  • Buffer messages on the client during disconnection and replay them in order upon reconnection
  • For horizontal scaling, use Redis pub/sub adapter (Socket.IO) or shared message broker for cross-server messaging
  • Set appropriate maxPayload limits on the server to prevent memory exhaustion from large messages
  • Close connections gracefully with proper close codes (1000 normal, 1001 going away) and reason strings