Install any skill in seconds. Free to start, no credit card required.
Get Started Free →WebSocket server and client patterns with heartbeat and reconnection. Use when implementing real-time features, debugging connection issues, or reviewing WebSocket code. Triggers on: WebSocket, wss, heartbeat, reconnect, real-time.
.claude/skills/aiskillstore-pitfalls-websocket/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-21 | ✗→✓ | ▲ Improved | -2% | 0% |
| case-16 | ✓→✗ | ▼ Worse | -10% | 0% |
| case-01 | ✓→✓ | = Same ✓ | 6% | 0% |
| case-02 | ✓→✓ | = Same ✓ | -19% | 0% |
| case-03 | ✓→✓ | = Same ✓ | -3% | 0% |
Common pitfalls and correct patterns for WebSocket implementations.
Check WebSocket server shares HTTP port.
Ensure ping/pong heartbeat is implemented.
Confirm exponential backoff reconnection logic.
typescriptconst wss = new WebSocketServer({ server: httpServer }); // Same port wss.on('connection', (ws) => { // Heartbeat ws.isAlive = true; ws.on('pong', () => { ws.isAlive = true; }); ws.on('message', (data) => { try { const msg = JSON.parse(data.toString()); // Validate message type } catch { ws.send(JSON.stringify({ error: 'Invalid message' })); } }); }); // Heartbeat interval setInterval(() => { wss.clients.forEach((ws) => { if (!ws.isAlive) return ws.terminate(); ws.isAlive = false; ws.ping(); }); }, 30000);
typescript// Client - reconnection logic with exponential backoff let attempt = 0; function connect() { const ws = new WebSocket(url); ws.onopen = () => { attempt = 0; // Reset on successful connection }; ws.onclose = () => { const delay = Math.min(1000 * Math.pow(2, attempt), 30000); attempt++; setTimeout(connect, delay); }; ws.onerror = (error) => { console.error('WebSocket error:', error); }; }
typescript// ✅ Always validate and type messages interface WsMessage { type: 'subscribe' | 'unsubscribe' | 'ping'; channel?: string; } function handleMessage(ws: WebSocket, data: string) { try { const msg: WsMessage = JSON.parse(data); switch (msg.type) { case 'subscribe': subscribeToChannel(ws, msg.channel); break; case 'ping': ws.send(JSON.stringify({ type: 'pong' })); break; default: ws.send(JSON.stringify({ error: 'Unknown message type' })); } } catch { ws.send(JSON.stringify({ error: 'Invalid JSON' })); } }
| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-01 | pass→pass | 16,269 | 13,108 | -19% | 1 | 1 | 0% | 2,106 | 2,240 | +6% | 0 | 0 | — |
case-02 | pass→pass | 15,706 | 3,691 | -76% | 1 | 1 | 0% | 1,561 | 1,262 | -19% | 0 | 0 | — |
case-03 | pass→pass | 11,092 | 7,297 | -34% | 1 | 1 | 0% | 1,933 | 1,871 | -3% | 0 | 0 | — |
case-04 | pass→pass | 13,045 | 9,836 | -25% | 1 | 1 | 0% | 1,434 | 1,560 | +9% | 0 | 0 | — |
case-05 | pass→pass | 11,005 | 2,542 | -77% | 1 | 1 | 0% | 2,068 | 1,175 | -43% | 0 | 0 | — |
case-06 | pass→pass | 10,339 | 5,823 | -44% | 1 | 1 | 0% | 1,930 | 1,807 | -6% | 0 | 0 | — |
case-07 | pass→pass | 17,703 | 5,794 | -67% | 1 | 1 | 0% | 2,522 | 1,658 | -34% | 0 | 0 | — |
case-08 | fail→fail | 12,771 | 13,818 | +8% | 1 | 1 | 0% | 2,196 | 1,951 | -11% | 0 | 0 | — |
case-09 | pass→pass | 21,094 | 11,953 | -43% | 1 | 1 | 0% | 3,034 | 3,055 | +1% | 0 | 0 | — |
case-10 | pass→pass | 19,227 | 15,329 | -20% | 1 | 1 | 0% | 2,455 | 2,489 | +1% | 0 | 0 | — |
case-11 | pass→pass | 16,462 | 4,757 | -71% | 1 | 1 | 0% | 1,850 | 1,413 | -24% | 0 | 0 | — |
case-12 | pass→pass | 12,900 | 7,579 | -41% | 1 | 1 | 0% | 1,427 | 1,112 | -22% | 0 | 0 | — |
case-13 | pass→pass | 11,823 | 4,945 | -58% | 1 | 1 | 0% | 1,258 | 1,400 | +11% | 0 | 0 | — |
case-14 | pass→pass | 6,632 | 6,585 | -1% | 1 | 1 | 0% | 1,183 | 1,288 | +9% | 0 | 0 | — |
case-15 | pass→pass | 11,473 | 7,383 | -36% | 1 | 1 | 0% | 2,073 | 2,056 | -1% | 0 | 0 | — |
case-16 | pass→fail | 15,337 | 10,433 | -32% | 1 | 1 | 0% | 1,904 | 1,717 | -10% | 0 | 0 | — |
case-17 | pass→pass | 17,129 | 3,727 | -78% | 1 | 1 | 0% | 2,027 | 1,354 | -33% | 0 | 0 | — |
case-18 | pass→pass | 13,493 | 3,438 | -75% | 1 | 1 | 0% | 1,354 | 1,203 | -11% | 0 | 0 | — |
case-19 | pass→pass | 9,501 | 8,126 | -14% | 1 | 1 | 0% | 575 | 1,080 | +88% | 0 | 0 | — |
case-20 | pass→pass | 23,854 | 15,630 | -34% | 1 | 1 | 0% | 3,046 | 2,619 | -14% | 0 | 0 | — |
case-21 | fail→pass | 22,895 | 16,815 | -27% | 1 | 1 | 0% | 3,428 | 3,359 | -2% | 0 | 0 | — |
case-22 | pass→pass | 11,108 | 9,712 | -13% | 1 | 1 | 0% | 960 | 1,448 | +51% | 0 | 0 | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 22 cases were attempted. The headline lift of 0 percentage points is the difference between those two pass rates over the 22 comparable cases. 1 case got worse with the skill loaded, and it is included in that figure.
Without the skill loaded, the model failed this case. With it loaded, the same prompt on the same model passed. This is one improved case from the latest verified run; every case, including any that regressed, is in the table above.
Other measured skills in the registry, with their headline benchmark lift.