Skip to Content
Client Lifecycle

Client Lifecycle

Creating a client

RUST
use std::{sync::Arc, time::Duration};use kalam_client::{AuthProvider, EventHandlers, KalamLinkClient}; let handlers = EventHandlers::new()    .on_connect(|| println!("connected"))    .on_disconnect(|reason| println!("disconnected: {reason}"))    .on_error(|error| eprintln!("connection error: {}", error.message)); let client = KalamLinkClient::builder()    .base_url("http://localhost:2900")    .auth(AuthProvider::jwt_token(token))    .timeout(Duration::from_secs(30))    .max_retries(3)    .event_handlers(handlers)    .build()?;

Notes:

  • build() returns a ready client handle. HTTP queries work immediately.
  • Live subscriptions additionally use the shared WebSocket.
  • base_url is required; everything else has defaults.

WebSocket lazy connect (default: true)

ConnectionOptions::ws_lazy_connect defaults to true. The shared WebSocket opens on the first live() or live_events() call, not at build() time.

Eager connect:

RUST
use kalam_client::ConnectionOptions; let client = KalamLinkClient::builder()    .base_url("http://localhost:2900")    .auth(AuthProvider::jwt_token(token))    .connection_options(ConnectionOptions::default().with_ws_lazy_connect(false))    .build()?; client.connect().await?; // opens socket immediately

Explicit connect and disconnect

RUST
client.connect().await?;assert!(client.is_connected().await); client.disconnect().await;

Call connect() before multiple subscriptions so they share one socket and one auth context.

Auth refresh model

  • Static AuthProvider on the builder is resolved at build time.
  • DynamicAuthProvider re-runs on connect/reconnect via fresh_auth().
  • auth_refresher handles TOKEN_EXPIRED during query/subscription operations.
  • set_auth / update_shared_auth push new credentials without rebuilding the client.

See Authentication and Auth-Aware Client.

Event handlers

EventHandlers callbacks:

CallbackWhen it fires
on_connectShared WebSocket becomes ready
on_disconnectConnection closes (DisconnectReason)
on_errorConnection or protocol errors (ConnectionError)
on_receive / on_sendOptional debug hooks for wire messages

Handlers are Arc-backed and safe to share across cloned clients.

Timeouts and retries

RUST
use kalam_client::KalamLinkTimeouts; let client = KalamLinkClient::builder()    .base_url("http://localhost:2900")    .timeout(Duration::from_secs(10))    .max_retries(2)    .timeouts(KalamLinkTimeouts::builder().receive_timeout(Duration::from_secs(15)).build())    .build()?;
  • timeout — HTTP request timeout (default 30s)
  • max_retries — retries for idempotent HTTP queries (default 3)
  • timeouts(...) — finer-grained send/receive limits for HTTP and WebSocket

Shutdown checklist

  1. live.close().await? / events.close().await? for each open subscription
  2. consumer.close().await? for each topic consumer
  3. client.disconnect().await to close the shared WebSocket

Long-running services should always close streams before dropping the client.

Cloning and sharing

KalamLinkClient is cloneable (Arc internally). Clone handles share connection state — useful for spawning multiple subscription tasks from one service.

Next

Last updated on