{"success":true,"name":"design-websocket-reconnection-before-the-first-disconnect","description":"Using WebSockets or live connections for real-time features.","category":"ai-brimind","path":"ai-brimind/design-websocket-reconnection-before-the-first-disconnect/SKILL.md","file":"SKILL.md","content":"---\nname: design-websocket-reconnection-before-the-first-disconnect\ndescription: \"Using WebSockets or live connections for real-time features.\"\n---\n\n# Design WebSocket Reconnection Before the First Disconnect\n\n## When to use\nThe app holds a persistent connection (WebSocket, SSE, socket library), and disconnect/reconnect/missed-messages behavior is undesigned.\n\n## Example request\nWiFi blips for 5 seconds → the socket dies silently; the UI looks live but updates stopped forever — no reconnect logic. Reconnect loop retries instantly → hundreds of attempts per second hammer the server during its outage — no backoff. Connection restored → messages sent during the gap are simply missing; the client's state is silently wrong — no catch-up mechanism.\n\n## Instructions\nAct like a developer who knows a persistent connection is a promise the network refuses to keep — mobile networks, sleeping laptops, and TV standby all kill sockets constantly. Design four parts: (1) DETECTION — know the connection is dead: handle close/error events, and use heartbeat/ping-pong because some deaths are silent (no event fires, the socket just goes quiet); a UI showing 'live' on a dead socket is lying; (2) RECONNECTION — automatic, with exponential backoff plus jitter (1s, 2s, 4s… capped), infinite patience, and immediate retry on explicit network-restored events; instant-retry loops DDoS your own recovering server; (3) CATCH-UP — reconnecting is not enough; messages during the gap are gone, so on reconnect either re-fetch current state from an API (simplest and usually right) or request messages since a last-seen id/timestamp; without this, every disconnect silently corrupts client state; (4) UI HONESTY — show the connection state (connected / reconnecting) so users understand why data froze, and queue or disable outgoing actions while down rather than dropping them. Check whether the socket library in use already provides reconnection and buffering before hand-rolling. Only proceed once pulling the network cable mid-session provably ends with: reconnected, state correct, user informed throughout.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #1182\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}