Skip to content

Live Queries

LiveQuery runs a query and pushes updates whenever the underlying data changes. Great for dashboards and feeds.

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();

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[];
};
unsub.unsubscribe(); // stops the live query

A live orders board:

const live = new LiveQuery(hub, db.from(orders).orderBy("createdAt", "desc"));
live.subscribe((change) => renderBoard(change.snapshot));
await live.start();
  • Scope live queries narrowly (filter + limit) — they re-run on writes.
  • Unsubscribe on unmount to free the subscription.
  • Pair with PresenceChannel for collaborative views.
  • Live-querying an unfiltered, huge table — every write re-scans it.
  • Forgetting start() — no updates arrive.
  • Presence — who’s viewing.
  • Cache — pair with query caching for read scaling.