Skip to Content
🚀 KalamDB v0.3.0-alpha2 is out — Learn more
SQL ReferenceSubscriptions & Live Queries

Subscriptions & Live Queries

KalamDB’s real-time engine pushes data changes to connected clients over WebSocket. This eliminates the need for polling and separate pub/sub infrastructure.

SUBSCRIBE TO

Subscribe to a table for real-time change notifications:

SUBSCRIBE TO <namespace>.<table_name> [WHERE <condition>] [OPTIONS (last_rows=<n>, batch_size=<n>, from_seq_id=<n>)];

Options

OptionDescription
last_rows=<n>Receive the last N rows as initial data
batch_size=<n>Maximum rows per notification batch
from_seq_id=<n>Resume from a specific sequence ID

Examples

-- Subscribe to all messages in a conversation SUBSCRIBE TO chat.messages WHERE conversation_id = 1 OPTIONS (last_rows=50); -- Subscribe to typing events with batching SUBSCRIBE TO chat.typing_events WHERE conversation_id = 1 OPTIONS (last_rows=10, batch_size=5); -- Resume from a specific point SUBSCRIBE TO chat.messages WHERE conversation_id = 1 OPTIONS (from_seq_id=1234);

KILL LIVE QUERY

Cancel an active subscription:

KILL LIVE QUERY '<subscription_id>';

Using Subscriptions via SDK

The recommended way to use real-time subscriptions is via the TypeScript SDK:

import { createClient, Auth } from 'kalam-link'; const client = createClient({ url: 'http://localhost:8080', auth: Auth.basic('admin', 'admin') }); await client.connect(); // Subscribe with SQL WHERE clause const unsub = await client.subscribeWithSql( 'SELECT * FROM chat.messages WHERE conversation_id = 1', (event) => { if (event.type === 'change') { console.log('New message:', event.rows); } }, { batch_size: 50 } ); // Simple table subscription const unsubTyping = await client.subscribe( 'chat.typing_events', (event) => { console.log('Typing:', event.change_type, event.rows); } ); // Check active subscription count console.log(`Active: ${client.getSubscriptionCount()}`); // Cleanup await unsub(); await unsubTyping(); await client.disconnect();

Using Subscriptions via CLI

In the interactive CLI, use the \subscribe prefix:

kalam> \subscribe SELECT * FROM chat.messages WHERE conversation_id = 1 OPTIONS (last_rows=20);

Press Ctrl+C to stop the subscription.

How It Works

  1. Client sends a SUBSCRIBE TO statement (via SQL, SDK, or WebSocket)
  2. KalamDB registers the subscription with filter conditions
  3. On every INSERT, UPDATE, or DELETE matching the filter, a change event is pushed
  4. Events are delivered over the persistent WebSocket connection
  5. Client can cancel with KILL LIVE QUERY or disconnect
Last updated on