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.

@appbricx/ui5 membersSDK referenceRealtime guide

On this page
  1. Live reads
  2. useLive
  3. useLiveRows
  4. Channels and presence
  5. useChannel
  6. usePresence
  7. useAppHandle
  8. Types

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 } = {}): void

Parameters

NameTypeDescription
tablerequiredT
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.
Example
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 }): LiveRows

Parameters

NameTypeDescription
apprequiredAppHandle | undefineduseAppHandle().
entityrequiredstringThe table.
whereoptionalAppQuery["where"]Equality filter.
optsoptional{ sort?: AppQuery["sort"]; limit?: number }sort and limit (default 50).
Returns
{ rows, loading, error }.
Errors
  • Never throws: see error.
Permissions
Each change is re-read as the subscriber under row-level security.
Example
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>): ChannelState

Parameters

NameTypeDescription
apprequiredAppHandle | undefined
namerequiredstring
keyoptionalstring
onoptionalRecord<string, (payload: unknown, from: AppRealtimeMember | null, event: string) => void>
Returns
{ send, joined, error, channel }. error says why it didn't join (sign in, a role, a row you can't read).
Errors
  • send rejects 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.
Example
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

NameTypeDescription
apprequiredAppHandle | undefined
namerequiredstring
keyoptionalstring
Returns
AppRealtimeMember[]
Errors
Does not throw.
Permissions
As useChannel.
Example
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 } = {}): AppHandle

Parameters

NameTypeDescription
optsoptional{ rowsLimit?: number }
Returns
AppHandle
Errors
  • Throws outside the app.
Permissions
As the signed-in user.
Example
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