[{"data":1,"prerenderedAt":4},["ShallowReactive",2],{"article-doc:docs\u002Fguides\u002F6.reconnection":3},"---\ntitle: Reconnect a Live Session\ndescription: Network disconnect, retry, and resume design.\n---\nRecommended design for handling WebSocket disconnects in realtime sessions.\n\n## Reconnect decision matrix\n\n| Close code | Meaning | Action |\n|---:|---|---|\n| 1000 | Normal close | Do not reconnect |\n| 4408 | Idle timeout | Reconnect immediately |\n| 1008 | Session duration limit | Reconnect immediately (new session) |\n| 1009 | Frame too large | Fix frame size, then reconnect |\n| 1011 | Client socket error | Reconnect with backoff |\n| 1013 | Upstream unavailable | Exponential backoff |\n\n## Exponential backoff\n\n```typescript\nfunction reconnectWithBackoff(url, maxRetries = 5) {\n  let attempt = 0;\n  let lastLanguage = 'zh';\n\n  function connect() {\n    const ws = new WebSocket(url);\n\n    ws.onopen = () => {\n      console.log('Connected');\n      attempt = 0; \u002F\u002F reset backoff on success\n      \u002F\u002F Re-send session configuration\n      ws.send(JSON.stringify({ type: 'session.update', language: lastLanguage }));\n    };\n\n    ws.onclose = (event) => {\n      if (event.code === 1000) return; \u002F\u002F normal close\n\n      if (attempt \u003C maxRetries) {\n        const delay = Math.min(1000 * Math.pow(2, attempt), 30000);\n        attempt++;\n        console.log(`Reconnecting in ${delay}ms (attempt ${attempt})`);\n        setTimeout(connect, delay);\n      }\n    };\n\n    ws.onmessage = (event) => {\n      \u002F\u002F Handle events as normal\n    };\n  }\n\n  connect();\n}\n```\n\n## Session continuation\n\nAfter reconnecting:\n- You get a **new** `session_id` — sessions do not resume\n- Re-send `session.update` to restore language, VAD, and other settings\n- Previous utterances are not re-delivered\n- If you need the complete transcript, accumulate segments client-side\n\n## Keep-alive strategies\n\nTo avoid idle timeout (4408):\n\n```typescript\n\u002F\u002F Send silent frames to keep connection alive\nfunction keepAlive(ws) {\n  setInterval(() => {\n    if (ws.readyState === WebSocket.OPEN) {\n      const silence = new ArrayBuffer(3200); \u002F\u002F 100ms of silence\n      ws.send(silence);\n    }\n  }, 5000); \u002F\u002F every 5 seconds\n}\n```\n\n## Session token refresh\n\nBrowser connections: session tokens expire in 60 seconds. If reconnection happens after token expiry:\n\n```typescript\nasync function connectWithFreshToken() {\n  const { token } = await fetch('\u002Fsession-token', { method: 'POST' }).then(r => r.json());\n  return new WebSocket(\n    `wss:\u002F\u002Faudio.lansonai.com\u002Fv1\u002Faudio\u002Ftranscriptions\u002Fstream?access_token=${token}`\n  );\n}\n```\n\n## Related\n\n- [Connection Lifecycle](\u002Fdocs\u002Frealtime\u002Fconnection-lifecycle) — close codes and timeouts\n- [Reconnection & Retries](\u002Fdocs\u002Fproduction\u002Freconnection-retries) — production retry strategies\n- [Authentication](\u002Fdocs\u002Fstart\u002Fauthentication) — session token refresh\n",1790059118952]