Skip to content

Auto reconnect - typescript sdk - #5927

Open
aasoni wants to merge 16 commits into
masterfrom
alessandro/auto-reconnect-typescript-sdk
Open

aasoni wants to merge 16 commits into
masterfrom
alessandro/auto-reconnect-typescript-sdk

Conversation

@aasoni

@aasoni aasoni commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Description of Changes

TypeScript SDK: automatic reconnection

Builds reconnection into DbConnection itself, so the connection object, the client cache, table handles and registered callbacks all stay usable across a reconnect and applications manage no replacement objects.

withAutomaticReconnect() enables this. Losing an established connection fires onDisconnect, each failed attempt fires onConnectError, and both now report the number of and delay before the next attempt, or undefined when the SDK will not retry. Retries are unlimited with exponential backoff (1s base, 30s cap, 0.5 jitter), stopping only on disconnect(), a failed initial connection, or a reconnect under a different identity.

Each reconnect is a new connection which fires onConnect again. Live subscriptions are replayed in one SubscribeBatch under fresh query set ids, and the results are reconciled against the stale cache with synthesized deletes, so rows unchanged across the outage produce no callbacks and changed rows produce ordinary update or delete callbacks.

A stable client-generated session_id is sent on every connection so the server can teardown the previous connection if still open (idle timeout didn't fire yet) before accepting a new one for the same session.

The token the server issued is saved and reused, keeping the identity stable for anonymous clients; withTokenProvider() supplies a fresh token when the saved one is close to expiring or was rejected.

Calls made while disconnected fail fast with DisconnectedError, and calls in flight when the connection dropped settle with UnknownCallResultError, since they may or may not have run.

Behavior is unchanged for connections which do not opt in.

API and ABI breaking changes

No

Rollback safety impact

Requires #5877 to have been deployed in a previous release

Expected complexity level and risk

3 - Change is limited to the typescript SDK and is opt in only (withAutomaticReconnect and withTokenProvider are the new APIs). Change is large but not overly complex.

Testing

Test app that fakes dropping connections and reconnecting and uses spacetime's own token provider to test token with expiry claim in the reconnect scenario as well.

aasoni and others added 8 commits August 26, 2026 17:39
Adds the two server-side changes needed for SDK auto-reconnect
(proposal 0037).

Connection replacement: a client which reconnects can now send a stable,
client-generated `session_id` query parameter. Each connection still
gets its own ConnectionId and its own lifecycle events. The session id
only identifies which earlier connection a new one supersedes. When a
connection claims a session held by a live connection of the same
identity, that connection's actor is stopped and its module-side
disconnect lifecycle is awaited before the new connection's client_connected,
so a module never observes two live connections for one session, nor the
old connection's client_disconnected after the new client_connected.

Batch subscribe: SubscribeBatch and SubscribeBatchApplied register
multiple query sets under one subscription-manager lock and evaluate
them at a single transaction snapshot. No transaction update can
interleave with the responses, so a reconnecting client can replay its
subscriptions and reconcile its cache against a consistent snapshot.
Per-set errors are reported in the response and the remaining sets still
apply
@aasoni
aasoni changed the base branch from master to alessandro/auto-reconnect-server-side September 11, 2026 13:31
@aasoni aasoni changed the title Alessandro/auto reconnect typescript sdk Auto reconnect - typescript sdk Sep 11, 2026
@aasoni
aasoni changed the base branch from alessandro/auto-reconnect-server-side to alessandro/refuse-takeover-for-same-session-and-teardown September 11, 2026 13:34
@aasoni
aasoni force-pushed the alessandro/auto-reconnect-typescript-sdk branch from c0eecbf to 812ee9b Compare September 11, 2026 13:41

@JasonAtClockwork JasonAtClockwork left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is working great in all my tests! I just have one callout that we'll want to fix with deepEqual() but that doesn't necessarily have to be fixed today.

We may also want another ticket to walk through all samples/templates to move all subscriptions out of onConnect. Especially for React/providers that this is will be opt-in by default.

Comment thread crates/bindings-typescript/src/sdk/table_cache.ts
Comment thread crates/bindings-typescript/src/sdk/db_connection_impl.ts
Comment thread crates/bindings-typescript/src/sdk/db_connection_impl.ts
@aasoni
aasoni changed the base branch from alessandro/refuse-takeover-for-same-session-and-teardown to master September 16, 2026 09:30
aasoni and others added 5 commits September 16, 2026 13:17
…onnect-typescript-sdk

# Conflicts:
#	crates/bindings-typescript/src/sdk/db_connection_impl.ts
Co-authored-by: Jason Larabie <jason@clockworklabs.io>
Signed-off-by: Alessandro Asoni <alessandro@clockworklabs.io>
Co-authored-by: Jason Larabie <jason@clockworklabs.io>
Signed-off-by: Alessandro Asoni <alessandro@clockworklabs.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants