Live updates
Keep a page's reads current as rows change, and talk on channels (chat, typing, who is here). Every change is re-read as each subscriber under row-level security, so nobody receives a row they couldn't read.
Tables listed in app.config.ts realtime.live stream their changes; any other table is followed through the slower row-changed topics. Channels are declared in realtime.channels with who may join and who may send.
useLiveRows, useChannel and usePresence take the handle useAppHandle() returns as their first argument.
Live reads
useLivefunction
Keeps the page's useQuery, useRow and useList reads of a table current while mounted: each change to a row the user may read (or could, before the change) refetches them. enabled: false pauses it.
useLive<T extends TableName>(table: T, opts: { enabled?: boolean } = {}): voidParameters
| Name | Type | Description |
|---|---|---|
tablerequired | T | |
optsoptional | { enabled?: boolean } |
- Returns
void- Errors
- Throws outside the app (a route or a component it renders).
- Permissions
- Changes are delivered only for rows the subscriber may read, re-read as them.
useLive("booking");
const { rows } = useQuery("booking", { where: { status: "requested" } });Source: internal/kernel/appsdk/ui/live.tsx:74
useLiveRowsfunction
The rows of a table matching where (equality), read as the user and then kept current from realtime events: inserts arrive at the top, and a row that leaves the filter or the user's access drops out.
useLiveRows(app: AppHandle | undefined, entity: string, where?: AppQuery["where"], opts?: { sort?: AppQuery["sort"]; limit?: number }): LiveRowsParameters
| Name | Type | Description |
|---|---|---|
apprequired | AppHandle | undefined | useAppHandle(). |
entityrequired | string | The table. |
whereoptional | AppQuery["where"] | Equality filter. |
optsoptional | { sort?: AppQuery["sort"]; limit?: number } | sort and limit (default 50). |
- Returns
{ rows, loading, error }.- Errors
- Never throws: see
error.
- Never throws: see
- Permissions
- Each change is re-read as the subscriber under row-level security.
const app = useAppHandle();
const { rows } = useLiveRows(app, "message", { room_id: roomId }, { limit: 100 });Source: internal/kernel/appsdk/ui/live.tsx:177
Channels and presence
useChannelfunction
Joins a channel declared in app.config.ts realtime.channels for the component's lifetime. on maps event names ("*": all) to handlers; send(event, payload) reaches everyone on it. A channel with table is per row: pass the row's id as key.
useChannel(app: AppHandle | undefined, name: string, key?: string, on?: Record<string, (payload: unknown, from: AppRealtimeMember | null, event: string) => void>): ChannelStateParameters
| Name | Type | Description |
|---|---|---|
apprequired | AppHandle | undefined | |
namerequired | string | |
keyoptional | string | |
onoptional | Record<string, (payload: unknown, from: AppRealtimeMember | null, event: string) => void> |
- Returns
{ send, joined, error, channel }.errorsays why it didn't join (sign in, a role, a row you can't read).- Errors
sendrejects before the channel is joined, or when the role may not send.
- Permissions
- The server checks every join (
access) and every send (publish). Messages aren't stored.
const app = useAppHandle();
const room = useChannel(app, "room", roomId, {
typing: (_payload, from) => setTyping(from?.name ?? "someone"),
});
<input onChange={() => void room.send("typing")} />Source: internal/kernel/appsdk/ui/live.tsx:229
usePresencefunction
Who is on a channel declared with presence: true, kept current: [{ user, client, name, role, self }]. [] when the channel has no presence or you can't join it.
usePresence(app: AppHandle | undefined, name: string, key?: string): AppRealtimeMember[]Parameters
| Name | Type | Description |
|---|---|---|
apprequired | AppHandle | undefined | |
namerequired | string | |
keyoptional | string |
- Returns
AppRealtimeMember[]- Errors
- Does not throw.
- Permissions
- As useChannel.
const here = usePresence(useAppHandle(), "room", roomId);
<p>{here.length} here</p>Source: internal/kernel/appsdk/ui/live.tsx:284
useAppHandlefunction
A handle on the app for the live hooks (useLiveRows, useChannel, usePresence take it first). It also carries the JSON kit's handle members, so components converted from a JSON app keep working; new code reads data with useQuery and writes with useTable or actions.
useAppHandle(opts: { rowsLimit?: number } = {}): AppHandleParameters
| Name | Type | Description |
|---|---|---|
optsoptional | { rowsLimit?: number } |
- Returns
AppHandle- Errors
- Throws outside the app.
- Permissions
- As the signed-in user.
const app = useAppHandle();
const room = useChannel(app, "lobby");Source: internal/kernel/appsdk/ui/handle.tsx:240
Types
type LiveRows = {
rows: Row[];
/** First load in flight. */
loading: boolean;
error: string | null;
};Source: internal/kernel/appsdk/ui/live.tsx:95
type ChannelState = {
/** Send to everyone on the channel (you included). */
send(event: string, payload?: unknown): Promise<void>;
/** Joined and ready to send. */
joined: boolean;
/** Why you couldn't join (sign in, role, a row you can't read…). */
error: string | null;
channel: AppRealtimeChannel | null;
};Source: internal/kernel/appsdk/ui/live.tsx:217