SDK Cookbook
This page is a practical recipe collection built from real SDK source usage and the examples/chat-with-ai implementation.
Recipe 1: Chat with AI pipeline (topic + consumer + subscriptions)
This mirrors the chat-with-ai architecture:
- User message inserted into
chat.messages - CDC routes inserts into topic
chat.ai_processing - Background service consumes topic and writes AI reply back to table
- Frontend receives both user and AI rows instantly via live subscription
1) SQL setup
2) Frontend subscription (React)
3) Node.js background processor (consumer)
4) Operational notes from source
executeAsUser(...)follows KalamDB’s role matrix; service workers can target service/user accounts, while direct USER-table queries remain scoped to the authenticated account.executeAsUser(...)is valid for USER and STREAM tables; shared tables still rely on their table policy.- Typing indicator updates are done via a stream table (
chat.typing_indicators). - Service loads WASM explicitly in Node contexts where needed.
The server-side table policy details are documented at /docs/server/architecture/table-types.
Recipe 2: Resumable subscriptions with sequence checkpoints
Use from plus the typed lastSeqId checkpoints returned by getSubscriptions().
Recipe 2.1: Payload parser guard for consumer handlers
When your topic source uses WITH (payload = 'full'), parse both wrapper and direct row forms:
Recipe 3: Typed query helpers for safer app code
Why this pattern helps:
- keeps SQL in one place
- maps array-row response into typed objects
- reduces unsafe indexing in UI code
Recipe 4: File attachments in messages
Then parse FILE metadata:
Recipe 5: Connection bootstrap for apps
This mirrors the provider pattern in chat-with-ai: Basic credentials are exchanged for JWT automatically before the realtime connection. Most apps can let the first live/subscription call connect lazily; call connect() only when you want to establish the shared WebSocket eagerly.
Recipe 6: Graceful shutdown for worker services
Recipe 7: Vector search for semantic retrieval
This matches the new vector-search flow tested in KalamDB core: embeddings live in regular SQL tables, vector indexes are created with DDL, and ranking happens in standard SQL.
1) Schema
2) Insert or update embeddings
3) Query nearest documents
Why this pattern helps:
- keeps semantic retrieval inside the same SQL surface as the rest of your app
- works with
TYPE = 'USER'for tenant-safe agent memory - lets you combine vectors, files, and structured rows without a separate vector service