websocket-client-resilience

Client-side WebSocket resilience patterns. Use when implementing or testing reconnection logic, heartbeat detection, command acknowledgment tracking, or message sequence gap detection for WebSocket clients. Trigger on: 'WebSocket reconnection', 'backoff with jitter', 'heartbeat missed', 'circuit breaker for connections', 'command acknowledgment', 'sequence gap detection', 'mobile timeout', 'connection drops', 'WebSocket resilience'. Skip for: server-side WebSocket handling, REST APIs, or non-real-time protocols.

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "websocket-client-resilience" with this command: npx skills add apankov1/quality-engineering/apankov1-quality-engineering-websocket-client-resilience

WebSocket Client Resilience

6 resilience patterns for WebSocket clients, designed for unreliable network conditions.

Mobile WebSocket connections fail in ways that local development environments don't surface. Tail latency on cellular networks can reach several seconds, which means aggressive health check timeouts (e.g. 5 seconds) may cause false disconnects on slow connections.

When to use: Implementing WebSocket client reconnection logic, building real-time features with persistent connections, mobile app WebSocket handling, any client that maintains long-lived server connections.

When not to use: Server-side WebSocket handlers, HTTP request/response patterns, Server-Sent Events (SSE).

Rationalizations (Do Not Skip)

RationalizationWhy It's WrongRequired Action
"Our users are on fast networks"Mobile users exist. Even desktop WiFi has transient blips.Test with throttled networks
"Simple retry is enough"Without jitter, all clients retry at once after an outageAdd randomized jitter
"One missed heartbeat means disconnected"Network blips last 1-3 seconds. Single miss = false positive.Use hysteresis (2+ misses)
"We'll add resilience later"Reconnection logic is foundational. Retrofitting it is much harder.Build it in from the start
"5 seconds is plenty of timeout"Mobile P99 is 5-8s. That "timeout" is normal latency for mobile.Use 10s+ for mobile

Included Utilities

// WebSocket resilience pattern implementations (zero dependencies)
import {
  getBackoffDelay,
  circuitBreakerTransition,
  shouldDisconnect,
  CommandAckTracker,
  detectSequenceGap,
  classifyTimeout,
} from './resilience.ts';

getBackoffDelay() accepts an optional RNG function for deterministic tests:

const lowJitter = getBackoffDelay(0, 1000, 30000, () => 0); // 750
const highJitter = getBackoffDelay(0, 1000, 30000, () => 1); // 1250

Quick Reference

PatternDetectFixSeverity
Backoff without jitterMath.pow(2, attempt) without Math.random()Add +/- 25% jittermust-fail
No circuit breakerReconnect without failure counterTrip after 5 failures, 60s cooldownmust-fail
Single heartbeat misssetTimeout disconnect without miss counterRequire 2+ missed heartbeatsshould-fail
No command ackws.send() without commandId trackingTrack pending commands, timeout at 30snice-to-have
No sequence trackingonmessage without sequence checkTrack lastReceivedSequence, detect gapsnice-to-have
Short mobile timeoutHealth timeout < 10sUse 10s+ for all health checksmust-fail

Coverage

PatternUtilityStatus
1. Backoff with jittergetBackoffDelay()Code + tests
2. Circuit breakercircuitBreakerTransition()Code + tests
3. Heartbeat hysteresisshouldDisconnect()Code + tests
4. Command acknowledgmentCommandAckTrackerCode + tests
5. Sequence gap detectiondetectSequenceGap()Code + tests
6. Mobile-aware timeoutsclassifyTimeout()Code + tests

All 6 patterns have executable utilities and tests.

Companion Skills

This skill provides client-side resilience patterns, not WebSocket server architecture guidance. For broader methodology:

  • Search websocket on skills.sh for server-side handlers, protocol design, and connection management
  • The circuit breaker is a state machine (closed/open/half-open) — use model-based-testing for systematic transition matrix coverage of all state pairs
  • Backoff, circuit breaker, and retry patterns need fault simulation — use fault-injection-testing for circuit breaker testing utilities and queue preservation assertions
  • Connection lifecycle events should log at correct levels — use observability-testing to assert structured log output on connect/disconnect/reconnect

Framework Adaptation

These patterns are framework-agnostic. They work with:

  • Browser: Native WebSocket, Socket.IO, ws library
  • React/Vue/Svelte: Wrap in composable/hook
  • React Native / Flutter: Same patterns, different APIs
  • Node.js: ws library for server-to-server WebSocket clients

The core principle: real-world network conditions are more variable than controlled environments. Design for mobile latency, not localhost.

See patterns.md for full before/after code examples and detection commands for each pattern.

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

General

pairwise-test-coverage

No summary provided by upstream source.

Repository SourceNeeds Review
General

barrier-concurrency-testing

No summary provided by upstream source.

Repository SourceNeeds Review
General

breaking-change-detector

No summary provided by upstream source.

Repository SourceNeeds Review
General

fault-injection-testing

No summary provided by upstream source.

Repository SourceNeeds Review