Live Queries
LiveQuery runs a query and pushes updates whenever the underlying data
changes. Great for dashboards and feeds.
Create a live query
Section titled “Create a live query”import { Hub, LiveQuery } from "@mountsqli/realtime";
const hub = new Hub();const live = new LiveQuery(hub, db.from(orders).where("status", "=", "open"));
const unsub = live.subscribe((change) => { console.log("orders changed:", change);});
await live.start();How it updates
Section titled “How it updates”When a write touches a table the query depends on, the Hub notifies the
LiveQuery, which re-runs and publishes a LiveChange diff.
type LiveChange<T> = { type: "insert" | "update" | "delete"; row: T; snapshot: T[];};Stop listening
Section titled “Stop listening”unsub.unsubscribe(); // stops the live queryReal-world example
Section titled “Real-world example”A live orders board:
const live = new LiveQuery(hub, db.from(orders).orderBy("createdAt", "desc"));live.subscribe((change) => renderBoard(change.snapshot));await live.start();Best practices
Section titled “Best practices”- Scope live queries narrowly (filter + limit) — they re-run on writes.
- Unsubscribe on unmount to free the subscription.
- Pair with
PresenceChannelfor collaborative views.
Common mistakes
Section titled “Common mistakes”- Live-querying an unfiltered, huge table — every write re-scans it.
- Forgetting
start()— no updates arrive.
