Skip to Content

Python Live Queries

The Python SDK has the same three realtime concepts as TypeScript and Dart, with Pythonic snake_case names.

Materialized rows

Use live() when your app wants current rows instead of raw change events.

python
live_rows = await client.live(    "SELECT id, body FROM chat.messages WHERE thread_id = 1",    last_rows=20,    limit=20,    key_columns=["id"],    on_checkpoint=lambda checkpoint: save_resume_token(checkpoint["last_seq_id"]),) async for rows in live_rows:    render(rows)

Use live_table() when SELECT * FROM table is enough.

python
tasks = await client.live_table(    "app.tasks",    last_rows=20,    key_columns=["id"],) async for rows in tasks:    render_tasks(rows)

Low-level events

Use live_events() when you need the protocol frames directly.

python
events = await client.live_events(    "SELECT * FROM chat.messages WHERE thread_id = 1",    batch_size=100,    last_rows=50,    on_checkpoint=lambda checkpoint: save_resume_token(checkpoint["last_seq_id"]),    on_error=lambda event: log_live_error(event),) async for event in events:    kind = event.get("type")    if kind == "subscription_ack":        print("opened", event["subscription_id"])    elif kind == "initial_data_batch":        hydrate(event["rows"])    elif kind == "change":        apply_change(event["change_type"], event.get("rows"), event.get("old_values"))    elif kind == "error":        break

Options

OptionApplies toMeaning
batch_sizeall live methodsMax rows per initial snapshot batch
last_rowsall live methodsInclude the last N rows before live changes begin
from_all live methodsResume from a known sequence ID
auto_fetch_batchesall live methodsAsk for remaining startup batches automatically
limitlive(), live_table()Cap the materialized row set kept by the SDK
key_columnslive(), live_table()Stable row identity columns when the query does not expose id
on_checkpointall live methodsPersist the latest applied sequence ID
on_errorlive_events()Receive low-level error frames separately

The checkpoint object contains subscription_id and last_seq_id strings. Feed last_seq_id back into from_ on the next session to resume.

Last updated on