Key takeaways
- Add live to AutoTable, useFindMany, useFindOne or useFindFirst, and the read runs again whenever the model's records change, whoever changed them.
- The page keeps one long poll open to its records route for all its live reads. It hears within about a second which models changed, never the records themselves, and reads them again through the route.
- Because the read goes through the route, permissions and the merchant's store apply as they do to any read.
- On a model that changes all the time, such as one a backfill is writing, a live read reads again on almost every answer. Filter it tightly, or leave live off.
A merchant opens your app's orders page while the Shopify sync is still bringing orders in. Another staff member approves a review in a second tab. A background job finishes and marks a record done. Without live queries, the page shows none of this until someone reloads it. A read refreshes on its own only after a write made from the same page.
Turning it on
Live queries need @inferencebureau/apps-sdk 0.15.0 or later. Pass live to AutoTable, or { live: true } to any read hook:
"use client";
import { AutoTable, useFindMany } from "@inferencebureau/apps-sdk/react";
export function Moderation() {
// Pending reviews, read again whenever any review changes.
const pending = useFindMany("review", { filter: { status: { equals: "pending" } } }, { live: true });
return (
<>
<h2>{pending.data?.records.length ?? 0} waiting</h2>
{/* New orders appear as the sync writes them. */}
<AutoTable model="shopifyOrder" columns={["name", "totalPrice", "financialStatus"]} live />
</>
);
}
useFindOne and useFindFirst take the same option. Every write counts, whoever makes it: the API, an action, a job, a Shopify sync or webhook, the dashboard, or a restore from history.
How the update arrives
Every live read on a page shares one request to the app's records route, the same /api/records route the hooks already read through. The request is a long poll. The route asks Inference Bureau which of the watched models changed since a cursor. The answer comes back within about a second of a change, or after up to 25 seconds with nothing.
The answer holds only model names, never records or ids. Each read that watches a named model then reads again through the route. So permissions.ib.ts and the merchant's store decide what the page sees, as they would for any read:
- In the Shopify admin, a page hears only of its own store's records of store-owned models.
- A role can watch only the models it can read.
- A role's per-record filter isn't applied to the change feed, so a customer's page may learn that some order changed. The read that follows applies the filter, so it returns only their own orders.
The watch stops while the tab is hidden and picks up from its cursor when the tab comes back, so a change made meanwhile still triggers a read. If the route is unreachable, the page retries, backing off up to 30 seconds. If the route refuses the watch, live is turned off for those models and the browser console says why. A route on an older SDK refuses it, and so does a route whose models list leaves the model out.
Limits
- 25 seconds per watch. The route's function has to be allowed to run that long. If your setup can't, use
serveRecords(request, api, { liveWait: 10 })to wait less. - 50 models per watch.
- Only the read's own model is watched. A review read that selects its product's title doesn't hear about changes to products.
- The watch isn't counted in your plan's requests, only its bytes in bandwidth, so an idle open tab costs nothing against the allowance. Each read that follows a change is a request through the app's hostname, as any read is.
When not to use it
A live read reads again for every change to its model, and a filter doesn't change that. A pending-reviews list still reads again when an approved review is edited, and gets the same answer back.
That's cheap on a model that changes a few times a minute. It's wasteful on one that a backfill or a large sync is writing hundreds of records a second into: every answer names the model, so the page reads again almost continuously until the writes stop. For those pages, leave live off and give people a refresh button (refetch on any hook). Or watch a model that changes less often, such as a status record the job updates when it finishes.
Live queries also aren't a feed of what changed. If server code needs to react to writes, use api.$changes. It's the same call the route makes:
import { createClient } from "@inferencebureau/apps-sdk";
const api = createClient();
let cursor: string | null = null;
for (;;) {
// Without a cursor this answers at once, with one to start from.
const r = await api.$changes({ models: ["shopifyOrder"], cursor, wait: 25 });
cursor = r.cursor;
if (r.models.length) await refreshDashboardTotals();
}
$changes tells you which models changed, not which records. If you need to act on each record, do it where the write happens: in the action or background job that makes it.
Frequently asked questions
Does a live query push records to the page?
No. The page hears only which models changed, then reads again through its own records route, so permissions and the store apply as for any read.
How quickly does a page update?
The long poll answers within about a second of a change. The read that follows takes as long as any read.
Does it keep running in a background tab?
No. The watch stops while the tab is hidden and resumes from its cursor when the tab is visible again, so changes made meanwhile still trigger a read.
Why isn't my live table updating?
Check the browser console. When the route refuses a watch, because it's on an SDK before 0.15.0, doesn't serve the model or the role can't read it, live is turned off for those models with a warning there.