Skip to content

feat(ts-sdk): add reconnect() method to DbConnectionImpl#4631

Closed
xkelxmc wants to merge 1 commit intoclockworklabs:masterfrom
xkelxmc:feat/ts-sdk-reconnect
Closed

feat(ts-sdk): add reconnect() method to DbConnectionImpl#4631
xkelxmc wants to merge 1 commit intoclockworklabs:masterfrom
xkelxmc:feat/ts-sdk-reconnect

Conversation

@xkelxmc
Copy link

@xkelxmc xkelxmc commented Mar 13, 2026

Description of Changes

Adds a reconnect(newToken?) method to DbConnectionImpl that reopens the WebSocket on the same connection object, preserving the client cache.

Problem: Long-lived browser tabs (games, dashboards) lose their SpacetimeDB connection due to network changes, token expiration, or browser throttling. Currently the only recovery path is creating a new DbConnection, which destroys the client cache — all useTable hooks return [], causing UI flashes and loading screens.

Solution: reconnect() stores WS creation config in a private wsConfig field, silently closes the old WS (nulling handlers to prevent spurious events), then creates a new one. useTable hooks keep returning cached data while subscriptions re-establish.

connection.onDisconnect(() => {
  const freshToken = await refreshAuthToken();
  connection.reconnect(freshToken);
});

API and ABI breaking changes

None. This is a purely additive change — one new public method on DbConnectionImpl.

Expected complexity level and risk

1 — The change is small (74 lines) and self-contained. It reuses the existing WS creation pattern from the constructor. No existing behavior is modified.

Testing

  • Call connection.disconnect() then connection.reconnect() — WS reconnects and onConnect fires
  • useTable hooks retain cached data during reconnect
  • Subscriptions re-establish after reconnect
  • reconnect(newToken) uses the new token for authentication

Add a reconnect() method that reopens the WebSocket on the same
connection object, preserving the client cache. This allows
applications to handle token refresh and network recovery without
losing table data or causing UI flashes.

Use case: long-lived browser tabs (e.g. games) where the WS may
drop due to network changes, sleep, or token expiration.
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@xkelxmc xkelxmc closed this Mar 13, 2026
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.

2 participants