broadcast
Send a realtime message from server code to every page listening on that channel with useLive, so screens update when data changes.
Usage
app/orders/repository.ts
import { broadcast } from "manh/realtime";
export async function moveOrder(user, id: number, to: string) {
// … check and write to the database first
broadcast("orders", { id, status: to }); // everyone listening to "orders"
broadcast(`user:${row.assigneeId}`, { type: "status", id }); // one signed-in user
return updated;
}app/orders/page.tsx
useLive("orders", () => refresh());Use it in .ts files only: handlers, crons, jobs.
Signature
manh/realtime
function broadcast(channel: string, data?: unknown): void;Parameters
channel: the channel name, starting with a lowercase letter or digit, made of lowercase letters, digits and: _ -, up to 64 characters. A bad name throws. Auser:<id>channel reaches only pages that proved that id with their sign-in token.data: the data to send, which must be JSON-serialisable. When omitted,nullis sent.
Returns
Nothing. Messages go out when the request returns its response, so call broadcast after the write.
All messages of one request together are capped at 32 KB. Above that, listeners still get a message on the right channel, but data is null.
Examples
Tell the stock screen when a sync job finishes:
app/stock/sync.cron.ts
import { broadcast } from "manh/realtime";
import { syncStock } from "./repository";
export const schedule = "*/15 * * * *";
export default async function () {
const changed = await syncStock();
if (changed > 0) broadcast("stock", { changed });
}- Any channel other than
user:<id>can be heard by anyone who opens the app. Send ids or a “changed” signal, never private data; pages reload through the API, which checks permissions. - Each plan caps the number of simultaneous realtime connections per app (Free 100).
Next steps
Last updated