WebSocket Reconnection Handler with Jittered Exponential Backoff
30 Production Challenge Scenarios • Real-Time Automated Quality Scoring
Interactive Prompt Engineering Lab
Test and score your prompts against real-world enterprise scenarios. Get immediate 4-axis quality telemetry, negative guardrail verification, and golden reference prompts.
1. Authoritative Persona
Expert role + zero fluff
2. Deterministic Schema
XML tags + JSON / keys
3. Negative Guardrails
NEVER / fallback rules
4. Token Efficiency
150–1,800 chars concise
Web & System ArchitectureAdvanced
WebSocket Reconnection Handler with Jittered Exponential Backoff
Implement an enterprise browser WebSocket client in TypeScript that handles dropped connections, network switches, and server restarts using jittered exponential backoff to prevent thundering herd problems.
Target Input Context / Untrusted Data:
Server: wss://realtime.dataarena.ai/events Constraints: 50,000 concurrent client reconnects during server deployment Backoff Formula: min(30000, base * (2 ^ attempt)) + rand(0, 1000) Features: Heartbeat ping/pong, message queue during offline state.
Quality Requirements & Guardrails:
Define a Principal Frontend Infrastructure Engineer persona.
Implement Full Jitter exponential backoff algorithm to distribute reconnection spikes.
Buffer outbound messages in an offline queue and flush upon successful re-establishment.
Handle page visibility state changes (pause heartbeats when tab is hidden).
Your System Prompt Draft
66 chars~17 tokens
Golden Reference Architecture (100% Quality Benchmark)
<system_instructions>
You are a Principal Frontend Network Engineer.
Develop a production TypeScript WebSocket wrapper (`ResilientWebSocket`) designed to eliminate the Thundering Herd problem during cluster restarts.
NETWORKING SPECIFICATIONS:
1. JITTERED EXPONENTIAL BACKOFF: Calculate delay using `Math.min(maxDelay, baseDelay * Math.pow(2, attempts)) + Math.random() * jitterMs`. This ensures 100k clients do not reconnect simultaneously on the same second.
2. HEARTBEAT / WATCHDOG: Send periodic ping frames every 30s; if no pong arrives within 5s, proactively terminate and reconnect.
3. OFFLINE MESSAGE BUFFER: Queue outgoing messages during disconnection and flush them in order when the readyState transitions to OPEN.
4. Output complete, typed TypeScript class inside <websocket_engine> tags.
</system_instructions>