Client Lifecycle
This page documents the lifecycle methods implemented in src/client.ts.
Construction
Validation at construction time:
urlis requiredauthProvideris required
See Authentication for the full auth guide.
WebSocket connection behavior
The SDK manages the WebSocket connection lifecycle automatically. connect() is
public, but most applications only need it for eager connection or manual
reconnect flows.
query()runs over HTTP and does not require a WebSocket connection.live(),liveTable(), andliveEvents()automatically establish the WebSocket connection on first use.
Connect
connect() explicitly establishes or restores the shared WebSocket connection.
It is safe to call more than once and deduplicates concurrent connection work.
Existing subscription state is preserved in the underlying client so reconnecting
the same instance can resume live queries.
Lifecycle handlers
createClient() and the KalamDBClient instance support three connection-lifecycle hooks:
onConnect(callback)fires when the shared realtime connection becomes healthy.onConnectionError(callback)fires for connection and auth/bootstrap failures.onError(callback)remains as a compatibility alias for connection-error handling.
Use onConnectionError for new code. onError still works, but it exists mainly so older code does not break.
If you do not register either handler, the SDK now logs connection/auth failures to the console by default instead of failing silently.
What reaches onConnectionError
The callback is used for both WebSocket connection failures and pre-connect failures that happen before the socket opens:
- invalid or malformed server URLs
- auth provider resolution failures
- login or token refresh failures used during connection/bootstrap
- unreachable server / refused connection cases
The event shape comes from the shared WASM client and includes:
message: normalized error messagerecoverable:truewhen the failure can reasonably be retried,falsefor fatal configuration/auth issues
wsLazyConnect (default: true)
By default, the WebSocket connection is lazy: it is deferred until the
first live(), liveTable(), or liveEvents() call. This avoids unnecessary
connections when the client is only used for HTTP queries.
Set wsLazyConnect: false to establish the connection eagerly during
initialization:
pingIntervalMs
Use pingIntervalMs to tune the WebSocket ping interval (default 5000).
disableCompression
By default the server sends gzip-compressed binary WebSocket frames. During
development you can disable compression to inspect raw JSON frames in browser
DevTools or wscat:
When enabled the client appends ?compress=false to the WebSocket URL and
the server responds with plain-text JSON frames for every message.
Do not use in production. Compression significantly reduces bandwidth for subscription payloads. Only enable for local debugging.
Initialization behavior
initialize():
- loads WASM runtime (
init(...)) - resolves initial credentials from
authProvider - creates underlying WASM
KalamClient - wires
authProvidercallback into WASM for WebSocket auth resolution and reconnects - calls
setDisableCompression(true)on WASM client when option is set - if
wsLazyConnect: false, establishes the WebSocket connection immediately
If eager initialization hits a connection/auth/bootstrap failure, the SDK emits onConnectionError before rethrowing.
You usually don’t call it manually; most methods initialize lazily.
Disconnect
Notes:
disconnect()closes the WebSocket and clears SDK-side subscription tracking.- After disconnecting, the next
subscribe()call will re-establish the connection.
Full teardown with shutdown()
Use shutdown() when you want to free the underlying WASM client and fully
reset local SDK state. This is the stronger cleanup option for long-running
Node.js processes, test suites, and explicit resource-management flows.
Reconnection controls
setReconnectDelay(initial, max) is passed as BigInt to WASM.
Subscription state helpers
These are SDK-level convenience methods around subscription bookkeeping.
Cleanup pattern
Use this pattern in long-running services to avoid leaked subscriptions.