pysxLearn by building

Runtime and resilience · Lesson 21 of 28

Disconnect and reconnect

Observe connection state and recover the server's current UI without replaying actions.

A temporary network interruption does not necessarily mean the server lost the graph. Reconnecting can reattach to that existing owner and reconcile the current state. Meanwhile, server-owned background work may have progressed.

The client reports disconnection and bounds its retry policy. It does not quietly accumulate an unlimited queue of clicks or promise to replay actions whose effects are uncertain. That would be a different consistency contract.

Understand the moving parts

Type a draft and increment the live count. Click **Disconnect for 5 seconds** to close the real WebSocket and pause retries briefly. It reconnects automatically. Keep this page open without reloading. The status indicator comes from the actual client connection, not a demonstration-only Signal.

The server clock deliberately keeps running. On a healthy reattach you should see its current total rather than a replay of every missed visual tick. After the retained owner expires, recovery may reconstruct or reset; the next lesson explains that separate boundary.

Walk through the code

import asyncio
from pysx import Fragment, own_task, push, pysx, signal
from uuid import uuid4


def app(*, clock: bool = True) -> Fragment:
    draft = signal("")
    count = signal(0)
    ticks = signal(0)
    generation = uuid4().hex[:10]

    async def run() -> None:
        while True:
            await asyncio.sleep(0.5)
            await push(lambda: ticks.update(lambda value: value + 1))

    if clock:
        own_task("lesson-clock", run)

    def increment(_: object) -> None:
        count.update(lambda value: value + 1)

    return pysx(t"""
        label(for="recovery-draft"): "Delivered draft"
        input(id="recovery-draft" bindValue={draft})
        output(id="recovery-echo"): {draft}
        button(id="recovery-increment" onClick={increment}): "Increment delivered count"
        output(id="recovery-count"): {count}
        output(id="recovery-ticks"): {ticks}
        p(id="recovery-setup"): {generation}
        p:
            "Inspect data-pysx-connection on the root during interruption."
    """)

The draft starts empty and the count at zero. The increment handler writes the count; an owned task publishes a clock tick every half-second. Transport reattachment repairs the existing graph without a second event queue. The exercise's connection status follows the actual client connection in both development and production.

Try it live

Type a draft, increment the count, then click **Disconnect for 5 seconds**. Watch the connection status and clock without reloading the document.

What to observe

The status changes from Connected to Disconnected, then back to Connected. The delivered draft and count remain after reattach, and the clock jumps to its current total. Actions taken while disconnected are rejected rather than replayed.

00

5cccaa1dd6

Use the real connection status indicator. Keep this document open during interruption.

Connecting…

Take this with you

Reconnect reconciles current state. Unsent actions are not a durable transaction log.