Auth-Aware Client
If your service uses expiring tokens or loads user context separately from raw auth, your Kalam client should follow the same lifecycle: build once, refresh credentials when auth changes, and start subscriptions only when the session is ready.
This avoids a common failure mode — opening live subscriptions before you know which user or tenant the worker operates as.
Recommended lifecycle
- Build a shared
KalamLinkClient(orArc<KalamLinkClient>) at service startup. - Resolve auth with
AuthProvider::jwt_token(...)or aDynamicAuthProvider. - Call
connect()before live work when you want the WebSocket warm. - Start
live()orconsumer()loops only after credentials and namespace context are known. - Call
disconnect(), close subscriptions/consumers, and drop the client on shutdown.
See also Authentication, Realtime Subscriptions, and Client Lifecycle.
Dynamic auth provider
Use DynamicAuthProvider when credentials must be fetched lazily on every connect or reconnect — OAuth/OIDC flows, secret managers, or rotating refresh tokens.
The provider is invoked when the client needs fresh credentials for connect or reconnect.
Shared client in a Tokio service
Refresh without rebuilding the client
When you control token rotation in application code:
Or push a known token:
Pair with auth_refresher on the builder for automatic TOKEN_EXPIRED recovery during queries — see Authentication.
When to call connect()
- HTTP-only workers — skip
connect()if you only callexecute_query. - Live-query services — call
connect()before the firstlive()/live_events()so all subscriptions share one socket. - Eager warm-up — set
ConnectionOptions::default().with_ws_lazy_connect(false)on the builder if the first subscription must not pay socket setup latency.