Events & listeners

Decouple "a thing happened" from "what should happen in response". A typed createEventBus: register listeners, then one emit fans out to all of them (async listeners awaited). Here, registering a user broadcasts an event to three independent listeners.

Tip
The bus is in-memory and per-V8-isolate: register listeners at module scope (once per isolate) and emit within the same request. For durable cross-process fan-out, use the SDK queue or realtime.

Simulate a signup

Trigger the event to watch the listeners react.

src/app/examples/events/page.tsxTSX
import { createEventBus } from "@bext-stack/framework/events";

type AppEvents = {
  "user.registered": { id: string; email: string };
  "order.paid": { orderId: string; amount: number };
};
const bus = createEventBus<AppEvents>();

// Decoupled listeners — the registration flow doesn't know about them.
bus.on("user.registered", (u) => sendWelcome(u.email));
bus.on("user.registered", (u) => provisionWorkspace(u.id));
bus.on("user.registered", (u) => trackSignup(u.id));

// One line fans out to all three (awaits async listeners):
await bus.emit("user.registered", { id, email });