Skip to Content
Overview

Live Queries

@kalamdb/client supports realtime subscriptions via WebSocket.

The client has exactly three realtime entry points:

  • live(sql, onRows, options?) for materialized rows from any supported SELECT.
  • liveTable(tableName, onRows, options?) for materialized rows from SELECT * FROM table.
  • liveEvents(sql, onEvent, options?) for low-level protocol events.

Which Mode Should I Choose?

ModeStart withBest forYou manage
simplelive() or liveTable()UI state, feeds, timelines, dashboards, chat windowsalmost nothing; the SDK materializes the row set for you
advancedliveEvents()custom event handling and protocol-aware realtime logicraw events, change_type, rows, old_values, and batch metadata

Use simple when you want the latest rows.

Use advanced when you want to own the subscription lifecycle and event handling.

Simple Mode

  • Read: Simple Realtime Mode
  • Start with live() or liveTable().
  • The SDK reconciles inserts, updates, and deletes into one current row set.
ts snippetTS
const unsub = await client.live(  "SELECT id, room, body, created_at FROM support.inbox WHERE room = 'main'",  (rows) => renderInbox(rows),  {    limit: 100,    lastRows: 100,  },);

Advanced Mode

  • Read: Advanced Realtime Mode
  • Start with liveEvents() for raw events.
  • This mode exposes change_type, rows, old_values, _seq, subscription_id, and batch_control directly.
ts snippetTS
const stop = await client.liveEvents(  'SELECT * FROM app.messages WHERE conversation_id = 42',  (event) => {    if (event.type === MessageType.InitialDataBatch) {      renderBatch(event.rows);    }  },  {    batchSize: 5,    autoFetchBatches: false,  },);

Shared Subscription Rules

  • Live SQL must stay within the strict supported shape: SELECT ... FROM ... WHERE ....
  • Do not include ORDER BY or LIMIT inside live() or liveEvents() SQL.
  • batchSize chunks the initial snapshot coming from the server.
  • lastRows controls how much history to rewind before live changes begin.
  • from resumes from a known SeqId checkpoint.
  • limit applies only to the high-level materialized row set that live() keeps after startup.

Next Reads

Last updated on