1:"$Sreact.fragment"
2:I[15579,["/_next/static/chunks/fd0661f1506dcbc6.js","/_next/static/chunks/94245cbda44972fe.js","/_next/static/chunks/af778fff4a0f4be6.js","/_next/static/chunks/b096b037d08e2f31.js"],"Navigation"]
3:I[3013,["/_next/static/chunks/fd0661f1506dcbc6.js","/_next/static/chunks/94245cbda44972fe.js","/_next/static/chunks/af778fff4a0f4be6.js","/_next/static/chunks/b096b037d08e2f31.js"],""]
23:I[41451,["/_next/static/chunks/fd0661f1506dcbc6.js","/_next/static/chunks/94245cbda44972fe.js","/_next/static/chunks/af778fff4a0f4be6.js","/_next/static/chunks/b096b037d08e2f31.js"],"Footer"]
24:I[47913,["/_next/static/chunks/316a3a63422f35de.js"],"OutletBoundary"]
25:"$Sreact.suspense"
:HL["/blog/posts/websocket-reconnect-loops/hero.jpg","image"]
:HL["/blog/posts/logo.png","image"]
0:{"buildId":"TlpKRvbES4zzM7LeczAM7","rsc":["$","$1","c",{"children":[[["$","$L2",null,{}],["$","main",null,{"className":"pt-20 md:pt-24","children":["$","article",null,{"children":[["$","header",null,{"className":"border-b border-border","children":["$","div",null,{"className":"container mx-auto px-6 py-14 md:py-20","children":[["$","$L3",null,{"href":"/blog","className":"mb-8 inline-flex items-center text-sm font-medium text-muted-foreground transition-colors hover:text-foreground","children":[["$","svg",null,{"xmlns":"http://www.w3.org/2000/svg","width":24,"height":24,"viewBox":"0 0 24 24","fill":"none","stroke":"currentColor","strokeWidth":2,"strokeLinecap":"round","strokeLinejoin":"round","className":"lucide lucide-arrow-left mr-2 h-4 w-4","aria-hidden":"true","children":[["$","path","1l729n",{"d":"m12 19-7-7 7-7"}],["$","path","x3x0zl",{"d":"M19 12H5"}],"$undefined"]}],"Blog"]}],["$","div",null,{"className":"grid gap-8 md:grid-cols-[1fr_0.9fr] md:items-start","children":[["$","div",null,{"className":"max-w-3xl","children":[["$","div",null,{"className":"mb-5 flex flex-wrap items-center gap-3 text-sm text-muted-foreground","children":[["$","span",null,{"className":"rounded-md border border-primary/40 bg-primary/10 px-2.5 py-1 text-primary","children":"Reliability"}],["$","span",null,{"children":"2026-W18"}],["$","span",null,{"aria-hidden":"true","children":"/"}],["$","span",null,{"children":"4 min read"}],null]}],["$","h1",null,{"className":"text-4xl font-bold leading-tight text-balance md:text-6xl","children":"Persistent WebSocket Listeners That Actually Stay Connected"}],["$","p",null,{"className":"mt-6 text-lg leading-relaxed text-muted-foreground md:text-xl","children":"Naive `async for msg in ws` dies silently on the first reconnect. The reliable pattern is an outer `while True` loop that owns the reconnect responsibility, with `ping_interval` and `ping_timeout` configured so half-open connections actually surface."}]]}],["$","div",null,{"className":"overflow-hidden rounded-lg border border-border bg-card","children":["$","div",null,{"className":"relative aspect-[16/9] overflow-hidden","children":[null,["$","img",null,{"src":"/blog/posts/websocket-reconnect-loops/hero.jpg","alt":"A coiled ethernet cable being gently uncoiled by a single hand against a warm-lit wooden desk, macro focus on the connector tip, soft studio lighting, no people visible above the wrist, editorial.","className":"h-full w-full object-cover"}],["$","img",null,{"src":"/blog/posts/logo.png","alt":"","aria-hidden":"true","className":"pointer-events-none absolute right-4 top-4 h-[50px] w-[50px] mix-blend-screen"}]]}]}]]}]]}]}],["$","div",null,{"className":"container mx-auto px-6 py-12 md:py-16","children":["$","div",null,{"className":"grid gap-10 lg:grid-cols-[minmax(0,1fr)_280px] lg:items-start","children":[["$","div",null,{"className":"max-w-3xl text-muted-foreground","children":[[["$","h2","h2-0",{"className":"mt-12 text-2xl font-semibold leading-snug text-foreground first:mt-0","children":"The problem"}],"\n",["$","p","p-0",{"className":"mt-4 text-base leading-8 md:text-lg first:mt-0 first:text-xl first:leading-relaxed first:text-foreground md:first:text-2xl","children":["A long-running process needs to consume events from a WebSocket server indefinitely. The server restarts, deployments happen, the connection drops. The naive approach — connect once and ",["$","code","code-0",{"className":"rounded bg-secondary px-1.5 py-0.5 text-[0.9em] text-foreground","children":"async for msg in ws"}]," — dies silently the first time the connection closes, and your listener is gone."]}],"\n",["$","h2","h2-1",{"className":"mt-12 text-2xl font-semibold leading-snug text-foreground first:mt-0","children":"The approach"}],"\n",["$","p","p-1",{"className":"mt-4 text-base leading-8 md:text-lg first:mt-0 first:text-xl first:leading-relaxed first:text-foreground md:first:text-2xl","children":["The reliable pattern uses a tight outer ","$L4"," loop that owns the reconnect responsibility, with the inner connection block handling only the happy path:"]}],"\n","$L5","\n","$L6","\n","$L7","\n","$L8","\n","$L9","\n","$La"],"$Lb",null]}],null]}]}]]}]}],"$Lc"],["$Ld","$Le","$Lf"],"$L10"]}],"loading":null,"isPartial":false}
4:["$","code","code-0",{"className":"rounded bg-secondary px-1.5 py-0.5 text-[0.9em] text-foreground","children":"while True"}]
5:["$","div","pre-0",{"style":{"background":"hsl(220, 13%, 18%)","color":"hsl(220, 14%, 71%)","textShadow":"0 1px rgba(0, 0, 0, 0.3)","fontFamily":"\"Fira Code\", \"Fira Mono\", Menlo, Consolas, \"DejaVu Sans Mono\", monospace","direction":"ltr","textAlign":"left","whiteSpace":"pre","wordSpacing":"normal","wordBreak":"normal","lineHeight":"1.5","MozTabSize":"2","OTabSize":"2","tabSize":"2","WebkitHyphens":"none","MozHyphens":"none","msHyphens":"none","hyphens":"none","padding":"1rem","margin":"0.5em 0","overflow":"auto","borderRadius":"0.5rem","marginTop":"1.25rem","marginBottom":0,"border":"1px solid hsl(var(--border))","fontSize":"0.875rem"},"children":["$","code",null,{"style":{"whiteSpace":"pre","fontFamily":"ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, Liberation Mono, Courier New, monospace"},"children":[false,[["$","span","code-segment-0",{"className":"token","style":{"color":"hsl(286, 60%, 67%)"},"children":["while"]}],["$","span","code-segment-1",{"style":{},"children":[" "]}],["$","span","code-segment-2",{"className":"token","style":{"color":"hsl(29, 54%, 61%)"},"children":["True"]}],["$","span","code-segment-3",{"className":"token","style":{"color":"hsl(220, 14%, 71%)"},"children":[":"]}],["$","span","code-segment-4",{"style":{},"children":["\n"]}],["$","span","code-segment-5",{"style":{},"children":["    ws "]}],["$","span","code-segment-6",{"className":"token","style":{"color":"hsl(207, 82%, 66%)"},"children":["="]}],["$","span","code-segment-7",{"style":{},"children":[" websocket"]}],["$","span","code-segment-8",{"className":"token","style":{"color":"hsl(220, 14%, 71%)"},"children":["."]}],["$","span","code-segment-9",{"style":{},"children":["WebSocketApp"]}],["$","span","code-segment-10",{"className":"token","style":{"color":"hsl(220, 14%, 71%)"},"children":["("]}],["$","span","code-segment-11",{"style":{},"children":["url"]}],["$","span","code-segment-12",{"className":"token","style":{"color":"hsl(220, 14%, 71%)"},"children":[","]}],["$","span","code-segment-13",{"style":{},"children":[" on_open"]}],["$","span","code-segment-14",{"className":"token","style":{"color":"hsl(207, 82%, 66%)"},"children":["="]}],["$","span","code-segment-15",{"className":"token","style":{"color":"hsl(220, 14%, 71%)"},"children":["."]}],["$","span","code-segment-16",{"className":"token","style":{"color":"hsl(220, 14%, 71%)"},"children":["."]}],["$","span","code-segment-17",{"className":"token","style":{"color":"hsl(220, 14%, 71%)"},"children":["."]}],["$","span","code-segment-18",{"className":"token","style":{"color":"hsl(220, 14%, 71%)"},"children":[","]}],["$","span","code-segment-19",{"style":{},"children":[" on_message"]}],["$","span","code-segment-20",{"className":"token","style":{"color":"hsl(207, 82%, 66%)"},"children":["="]}],["$","span","code-segment-21",{"className":"token","style":{"color":"hsl(220, 14%, 71%)"},"children":["."]}],["$","span","code-segment-22",{"className":"token","style":{"color":"hsl(220, 14%, 71%)"},"children":["."]}],["$","span","code-segment-23",{"className":"token","style":{"color":"hsl(220, 14%, 71%)"},"children":["."]}],["$","span","code-segment-24",{"className":"token","style":{"color":"hsl(220, 14%, 71%)"},"children":[","]}],["$","span","code-segment-25",{"style":{},"children":[" on_close"]}],["$","span","code-segment-26",{"className":"token","style":{"color":"hsl(207, 82%, 66%)"},"children":["="]}],["$","span","code-segment-27",{"className":"token","style":{"color":"hsl(220, 14%, 71%)"},"children":["."]}],["$","span","code-segment-28",{"className":"token","style":{"color":"hsl(220, 14%, 71%)"},"children":["."]}],["$","span","code-segment-29",{"className":"token","style":{"color":"hsl(220, 14%, 71%)"},"children":["."]}],["$","span","code-segment-30",{"className":"token","style":{"color":"hsl(220, 14%, 71%)"},"children":[","]}],["$","span","code-segment-31",{"style":{},"children":[" on_error"]}],["$","span","code-segment-32",{"className":"token","style":{"color":"hsl(207, 82%, 66%)"},"children":["="]}],["$","span","code-segment-33",{"className":"token","style":{"color":"hsl(220, 14%, 71%)"},"children":["."]}],["$","span","code-segment-34",{"className":"token","style":{"color":"hsl(220, 14%, 71%)"},"children":["."]}],["$","span","code-segment-35",{"className":"token","style":{"color":"hsl(220, 14%, 71%)"},"children":["."]}],["$","span","code-segment-36",{"className":"token","style":{"color":"hsl(220, 14%, 71%)"},"children":[")"]}],["$","span","code-segment-37",{"style":{},"children":["\n"]}],["$","span","code-segment-38",{"style":{},"children":["    ws"]}],"$L11","$L12","$L13","$L14","$L15","$L16","$L17","$L18","$L19","$L1a","$L1b","$L1c","$L1d","$L1e","$L1f","$L20","$L21","$L22"]]}]}]
6:["$","p","p-2",{"className":"mt-4 text-base leading-8 md:text-lg first:mt-0 first:text-xl first:leading-relaxed first:text-foreground md:first:text-2xl","children":[["$","code","code-0",{"className":"rounded bg-secondary px-1.5 py-0.5 text-[0.9em] text-foreground","children":"run_forever"}]," blocks until the connection closes for any reason — clean close, server restart, network drop, anything. When it returns, you sleep briefly and reconnect. The ",["$","code","code-1",{"className":"rounded bg-secondary px-1.5 py-0.5 text-[0.9em] text-foreground","children":"on_close"}]," handler logs the close code so you have a record, but nothing in it needs to decide whether to reconnect — the outer loop handles that unconditionally."]}]
7:["$","p","p-3",{"className":"mt-4 text-base leading-8 md:text-lg first:mt-0 first:text-xl first:leading-relaxed first:text-foreground md:first:text-2xl","children":["The ",["$","code","code-0",{"className":"rounded bg-secondary px-1.5 py-0.5 text-[0.9em] text-foreground","children":"ping_interval"}]," and ",["$","code","code-1",{"className":"rounded bg-secondary px-1.5 py-0.5 text-[0.9em] text-foreground","children":"ping_timeout"}]," parameters matter more than they look. Without them, a half-open connection (TCP ACK but no application data) can leave the client sitting silently for minutes before it detects the drop. With ",["$","code","code-2",{"className":"rounded bg-secondary px-1.5 py-0.5 text-[0.9em] text-foreground","children":"ping_interval=30, ping_timeout=10"}],", the client sends a WebSocket ping every 30 seconds and expects a pong within 10 — the connection closes and reconnects within 40 seconds of a silent failure rather than never."]}]
8:["$","p","p-4",{"className":"mt-4 text-base leading-8 md:text-lg first:mt-0 first:text-xl first:leading-relaxed first:text-foreground md:first:text-2xl","children":["For the threaded version (",["$","code","code-0",{"className":"rounded bg-secondary px-1.5 py-0.5 text-[0.9em] text-foreground","children":"websocket-client"}],"), this pattern is synchronous and pairs with a daemon thread so it doesn't block the main process. For async (",["$","code","code-1",{"className":"rounded bg-secondary px-1.5 py-0.5 text-[0.9em] text-foreground","children":"websockets"}],"), replace ",["$","code","code-2",{"className":"rounded bg-secondary px-1.5 py-0.5 text-[0.9em] text-foreground","children":"run_forever"}]," with ",["$","code","code-3",{"className":"rounded bg-secondary px-1.5 py-0.5 text-[0.9em] text-foreground","children":"async with websockets.connect(url) as ws: async for msg in ws: ..."}]," inside a ",["$","code","code-4",{"className":"rounded bg-secondary px-1.5 py-0.5 text-[0.9em] text-foreground","children":"try/except Exception"}]," block, then ",["$","code","code-5",{"className":"rounded bg-secondary px-1.5 py-0.5 text-[0.9em] text-foreground","children":"await asyncio.sleep(5)"}]," in the outer loop."]}]
9:["$","h2","h2-2",{"className":"mt-12 text-2xl font-semibold leading-snug text-foreground first:mt-0","children":"What I learned"}]
a:["$","p","p-5",{"className":"mt-4 text-base leading-8 md:text-lg first:mt-0 first:text-xl first:leading-relaxed first:text-foreground md:first:text-2xl","children":["The ping parameters are the most commonly skipped configuration and the most consequential omission. A server that closes connections cleanly on restart is easy to detect — ",["$","code","code-0",{"className":"rounded bg-secondary px-1.5 py-0.5 text-[0.9em] text-foreground","children":"on_close"}]," fires immediately. A server that goes silent mid-connection (network partition, crashed process, half-open TCP) is invisible without active ping/pong. Setting ",["$","code","code-1",{"className":"rounded bg-secondary px-1.5 py-0.5 text-[0.9em] text-foreground","children":"ping_interval"}]," and ",["$","code","code-2",{"className":"rounded bg-secondary px-1.5 py-0.5 text-[0.9em] text-foreground","children":"ping_timeout"}]," turns the reconnect loop from \"handles clean closes\" into \"handles all close scenarios including silent failures.\""]}]
b:["$","div",null,{"className":"mt-14 border-t border-border pt-8","children":["$","$L3",null,{"href":"/platform","className":"inline-flex items-center text-sm font-medium text-primary transition-colors hover:text-primary/80","children":["Start a build",["$","svg",null,{"xmlns":"http://www.w3.org/2000/svg","width":24,"height":24,"viewBox":"0 0 24 24","fill":"none","stroke":"currentColor","strokeWidth":2,"strokeLinecap":"round","strokeLinejoin":"round","className":"lucide lucide-arrow-right ml-2 h-4 w-4","aria-hidden":"true","children":[["$","path","1ays0h",{"d":"M5 12h14"}],["$","path","xquz4c",{"d":"m12 5 7 7-7 7"}],"$undefined"]}]]}]}]
c:["$","$L23",null,{}]
d:["$","script","script-0",{"src":"/_next/static/chunks/94245cbda44972fe.js","async":true}]
e:["$","script","script-1",{"src":"/_next/static/chunks/af778fff4a0f4be6.js","async":true}]
f:["$","script","script-2",{"src":"/_next/static/chunks/b096b037d08e2f31.js","async":true}]
10:["$","$L24",null,{"children":["$","$25",null,{"name":"Next.MetadataOutlet","children":"$@26"}]}]
11:["$","span","code-segment-39",{"className":"token","style":{"color":"hsl(220, 14%, 71%)"},"children":["."]}]
12:["$","span","code-segment-40",{"style":{},"children":["run_forever"]}]
13:["$","span","code-segment-41",{"className":"token","style":{"color":"hsl(220, 14%, 71%)"},"children":["("]}]
14:["$","span","code-segment-42",{"style":{},"children":["ping_interval"]}]
15:["$","span","code-segment-43",{"className":"token","style":{"color":"hsl(207, 82%, 66%)"},"children":["="]}]
16:["$","span","code-segment-44",{"className":"token","style":{"color":"hsl(29, 54%, 61%)"},"children":["30"]}]
17:["$","span","code-segment-45",{"className":"token","style":{"color":"hsl(220, 14%, 71%)"},"children":[","]}]
18:["$","span","code-segment-46",{"style":{},"children":[" ping_timeout"]}]
19:["$","span","code-segment-47",{"className":"token","style":{"color":"hsl(207, 82%, 66%)"},"children":["="]}]
1a:["$","span","code-segment-48",{"className":"token","style":{"color":"hsl(29, 54%, 61%)"},"children":["10"]}]
1b:["$","span","code-segment-49",{"className":"token","style":{"color":"hsl(220, 14%, 71%)"},"children":[")"]}]
1c:["$","span","code-segment-50",{"style":{},"children":["\n"]}]
1d:["$","span","code-segment-51",{"style":{},"children":["    time"]}]
1e:["$","span","code-segment-52",{"className":"token","style":{"color":"hsl(220, 14%, 71%)"},"children":["."]}]
1f:["$","span","code-segment-53",{"style":{},"children":["sleep"]}]
20:["$","span","code-segment-54",{"className":"token","style":{"color":"hsl(220, 14%, 71%)"},"children":["("]}]
21:["$","span","code-segment-55",{"className":"token","style":{"color":"hsl(29, 54%, 61%)"},"children":["5"]}]
22:["$","span","code-segment-56",{"className":"token","style":{"color":"hsl(220, 14%, 71%)"},"children":[")"]}]
26:null
