WebSocket Real-Time Communication
Overview
WebSockets provide persistent bidirectional communication between clients and servers. This skill should be invoked when building real-time applications like chat, live dashboards, or collaborative tools.
Core Principles
- Persistent Connection: Single connection for bidirectional communication
- Message Protocol: Define message format for your application
- Connection Lifecycle: Handle connect, disconnect, error events
- Reconnection: Implement automatic reconnection
Preparation Checklist
- Choose WebSocket library (ws, Socket.io)
- Design message protocol
- Plan connection handling
- Design for scaling
Step-by-Step Process
- Server: Set up WebSocket server
- Upgrade: Handle HTTP to WebSocket upgrade
- Message: Implement send/receive logic
- Lifecycle: Handle connect/disconnect
- Reconnect: Add reconnection logic
- Scale: Plan for horizontal scaling
Do's and Don'ts
- ✅ Do use secure WebSocket (wss://)
- ✅ Do implement heartbeat/ping-pong
- ✅ Do handle reconnection
- ❌ Don't send sensitive data unencrypted
- ❌ Don't skip error handling
- ❌ Don't ignore scaling challenges
Anti-Patterns
- No Heartbeat: Not detecting dead connections
- No Reconnection: Not handling dropped connections
- Large Messages: Sending too much data
- No Error Handling: Ignoring errors