Autorun AI Docs

11. Frontend Execution

These two functions push a named message from a running process to the browser, where a client-side handler reacts to it. Use them to refresh a table after a background job, open a dialog, or nudge the UI once server-side work finishes.

You are sending a message, not calling a function

The process does not execute anything in the browser directly. It emits a functionId with an optional payload; whatever client-side handler is registered for that id decides what happens.


SendFunctionToUserAsync(userId, functionId, payload?)

async · returns true

Sends the message to one user's open sessions.

Prop

Type

await context.functions.SendFunctionToUserAsync(
  String(context.claims.userId),
  "RefreshProject",
  { projectId: context.record.ID, status: context.record.Status }
);

BroadcastFunctionAsync(functionId, payload?)

async · returns true

Sends the message to every connected user in the application.

Prop

Type

/** @event scheduled @schedule 0 3 * * * */
await context.functions.BroadcastFunctionAsync("RefreshDashboard", { reason: "nightly-sync" });

Delivery is not confirmed

Both functions always return true

The return value means "the message was handed off", nothing more. It is true even when the user is offline, has no session open, or has no handler registered for that functionId. There is no delivery receipt and no error to catch.

Treat these as best-effort UI hints. Anything that must happen reliably belongs in the database, where the UI will pick it up on its next read:

// Durable: the record carries the state
await context.functions.update("jobs", jobId, { Status: "complete", CompletedAt: new Date().toISOString() });

// Best-effort: nudge the UI to refresh right now
await context.functions.SendFunctionToUserAsync(String(job.CreateUser.ID), "RefreshJobs", { jobId: jobId });

Payload

The payload is serialized as-is. Keep it small and send identifiers rather than whole records — the client can read the current data itself, which also avoids showing a stale snapshot.

// Prefer this
await context.functions.SendFunctionToUserAsync(userId, "InvoicePaid", { invoiceId: invoice.ID });

// Rather than this
await context.functions.SendFunctionToUserAsync(userId, "InvoicePaid", invoice);

Omit the payload entirely when the signal alone is enough:

await context.functions.BroadcastFunctionAsync("ReloadSettings");