Overview
Provides complete guidance and code for integrating native WebSockets or Socket.io into web apps, including reconnection with exponential backoff, message envelope format, React hook for lifecycle management, server-side setup (Node/ws or Socket.io), error handling, and authentication patterns for real-time features.
When to Use This Skill
- Adding live chat, notifications, presence indicators, collaborative editing, or live data dashboards.
- The user mentions "real-time", "WebSocket", "live updates", "push", or "socket".
Prerequisites
- Backend that can run a WebSocket server (Node.js, Deno, or cloud service like Pusher, Ably, Supabase Realtime).
- Frontend framework (React, vanilla, etc.).
- For production: load balancer that supports WebSocket upgrade (Nginx, ALB, etc.).
Steps
- Choose transport: Native WebSocket (lightweight) vs Socket.io (more features, fallback).
- Define message protocol (JSON envelope):
{ "type": "chat.message", "payload": {...}, "id": "uuid", "timestamp": "..." }
- Client implementation:
- Connection, onopen, onmessage, onerror, onclose.
- Reconnection logic with backoff (max 5 attempts, then show "reconnecting" UI).
- Heartbeat / ping-pong for keepalive.
- React hook (
useWebSocket): manages connection, auto-reconnect, typed message handlers.
- Server side: auth on connection (JWT in query or subprotocol), room management, broadcast.
- Security: validate all incoming messages, rate limit, authenticate every connection.
- Output: full hook, example chat component, server route, deployment notes.
Examples
Complete useWebSocket.ts hook, a simple chat UI, and a minimal Node + ws server example are provided.
Edge Cases & Error Handling
- Network drops: exponential backoff + user notification.
- Auth expiration: refresh token and reconnect.
- Large messages: chunk or use binary when appropriate.
- Browser tab sleep: handle visibilitychange to pause/reconnect.
Verification
- Open two browser tabs — messages appear in real time.
- Kill the server — client shows reconnecting UI and recovers when server restarts.
- Check browser console for clean connection lifecycle.
- Success: Real-time features work reliably with graceful degradation.
References
1---2name: websocket-integration3description: Integrates WebSocket real-time communication into a web application. Use when implementing live features like chat, notifications, collaborative editing, or live dashboards.4license: Apache-2.05---67## Overview89Provides complete guidance and code for integrating native WebSockets or Socket.io into web apps, including reconnection with exponential backoff, message envelope format, React hook for lifecycle management, server-side setup (Node/ws or Socket.io), error handling, and authentication patterns for real-time features.1011## When to Use This Skill1213- Adding live chat, notifications, presence indicators, collaborative editing, or live data dashboards.14- The user mentions "real-time", "WebSocket", "live updates", "push", or "socket".1516## Prerequisites1718- Backend that can run a WebSocket server (Node.js, Deno, or cloud service like Pusher, Ably, Supabase Realtime).19- Frontend framework (React, vanilla, etc.).20- For production: load balancer that supports WebSocket upgrade (Nginx, ALB, etc.).2122## Steps23241. **Choose transport**: Native WebSocket (lightweight) vs Socket.io (more features, fallback).252. **Define message protocol** (JSON envelope):26 ```json27 { "type": "chat.message", "payload": {...}, "id": "uuid", "timestamp": "..." }28 ```293. **Client implementation**:30 - Connection, onopen, onmessage, onerror, onclose.31 - Reconnection logic with backoff (max 5 attempts, then show "reconnecting" UI).32 - Heartbeat / ping-pong for keepalive.334. **React hook** (`useWebSocket`): manages connection, auto-reconnect, typed message handlers.345. **Server side**: auth on connection (JWT in query or subprotocol), room management, broadcast.356. **Security**: validate all incoming messages, rate limit, authenticate every connection.367. **Output**: full hook, example chat component, server route, deployment notes.3738## Examples3940Complete `useWebSocket.ts` hook, a simple chat UI, and a minimal Node + ws server example are provided.4142## Edge Cases & Error Handling4344- Network drops: exponential backoff + user notification.45- Auth expiration: refresh token and reconnect.46- Large messages: chunk or use binary when appropriate.47- Browser tab sleep: handle visibilitychange to pause/reconnect.4849## Verification50511. Open two browser tabs — messages appear in real time.522. Kill the server — client shows reconnecting UI and recovers when server restarts.533. Check browser console for clean connection lifecycle.544. Success: Real-time features work reliably with graceful degradation.5556## References5758- [MDN WebSocket API](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket)59- [Socket.io Docs](https://socket.io/docs/v4/)60- [ws library (Node)](https://github.com/websockets/ws)