---
title: Events
description: A mutation emits an event, and its listeners run after commit.
---

An event records something that happened in a mutation so listeners can react to it.

<Diagram caption="The registry prepares listener jobs in the transaction and delivers them only after commit.">
  <Flow>
    <Node title="Mutation" subtitle="Changes the app's data" />
    <Arrow n={1} label="emit" />
    <Node title="Event" subtitle="Holds a payload snapshot" />
    <Arrow n={2} label="prepare" />
    <Node title="Job" subtitle="One per matching listener" />
    <Arrow n={3} label="after commit" />
    <Node title="Listener" subtitle="Runs with its scope" tone="accent" />
  </Flow>
</Diagram>

```ts twoslash
import { init } from "logic-sdk";
import { z } from "zod";

type Scope = { actor: string | null };
type Services = { mailer: { sendConfirmation: (id: string) => Promise<void> } };
const { event, mutation, listener } = init<{ scope: Scope; services: Services }>();
declare function confirmBooking(ctx: unknown, bookingId: string): Promise<{ id: string }>;
// ---cut---
export const confirmed = event({ payload: z.object({ bookingId: z.uuid() }) });

export const confirm = mutation({
  input: z.object({ bookingId: z.uuid() }),
  async handler(ctx, { bookingId }) {
    const booking = await confirmBooking(ctx, bookingId);
    ctx.emit(confirmed, { bookingId: booking.id });
    return booking;
  },
});

export const sendConfirmation = listener({
  event: () => confirmed,
  async handler(ctx, { bookingId }) {
    await ctx.services.mailer.sendConfirmation(bookingId);
  },
});
```

- **Emit (1).** Pass plain JSON. `ctx.emit` takes a snapshot that the registry validates before commit.
- **Prepare (2).** The registry makes a job per matching listener. See [`when`](/docs/concepts/jobs#listener-options).
- **Deliver (3).** After commit, the listener gets the parsed payload. Each mutation it runs has its own transaction.
- **Registration.** Export both definitions. Use `event: () => confirmed` to defer the reference.
- **Idempotency.** Record processed `ctx.event.id` values per listener. All listeners of one event share its ID.
- **Failures.** Production reports listener failures to `onError`. The call still succeeds. Inline tests make it throw.

## Event records

`ctx.emit` returns `{ id, name, payload, occurredAt }`. Save the ID in the mutation to match a row to the event.

`EventOf<typeof registry>` uses unparsed payload types. Reuse the event's `payload` schema to check stored rows. Upgrade old payloads in your app.

## Serverless

Set `waitUntil` on the registry to continue delivery after the response. See [Frameworks](/docs/guides/frameworks).

## Durability and loops

Use a [durable adapter](/docs/reference/adapters) to keep jobs if the process crashes after commit.

`jobs.maxEventDepth` defaults to eight hops. Events and due-now calls count. Delayed calls start a new chain. The registry reports and skips work past the limit.

## See also

[Transactions](/docs/concepts/transactions), [Jobs](/docs/concepts/jobs), [Testing](/docs/guides/testing)
