> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bijection.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Publish and follow application events

> Write an event in the business transaction and handle it once per follower.

An event states a fact, such as an order shipping. Publish it in the
mutation that makes it true, so the business write and the event commit
together. A task asks a performer to do work and settles separately; an
event is a fact other code reacts to.

## Declare and publish

`event({ fields, label, type })` from `@bijectionhq/library-events`
declares one append-only table of events. Place the value in the app tree;
its table is `<path>.events`. Choose a label, or pass `per: family` to
place each event under the family's member.

```ts bijection/shipping.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { label, mutation, t } from "@bijectionhq/bijection";
import { event } from "@bijectionhq/library-events";
import { clerk } from "./roles";
import { orders } from "./schema";

export const shipped = event({
  label: label.public,
  type: "order.shipped",
  fields: { order: t.string, carrier: t.string },
});

export const ship = mutation(
  { allow: [clerk], args: { order: t.key(orders), carrier: t.string } },
  async (ctx, { order, carrier }) => {
    await ctx.patch(orders(order), { state: "shipped" });
    await shipped.publish(ctx, { order, carrier });
  },
);
```

`publish` answers the event's minted key. Each publish is a distinct event,
even when two payloads and instants are equal.

## Follow

`follow(event, { roles, allow, batch }, handler)` is a reaction on the
event table's change log (`on: shipped.events.changes`). The host runs it
once per committed event, in commit order, with the `roles` you give it.
The handler runs in the delivery's transaction and receives the event
`{ id, type, data, occurredAt, version }` and `{ once }`, the follower's
identity for this event. Pass that `once` to anything the handler submits.

```ts bijection/app.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { app } from "@bijectionhq/bijection";
import { webhooks } from "@bijectionhq/app-webhooks";
import { follow, toWebhooks } from "@bijectionhq/library-events";
import { notifier } from "./roles";
import { ship, shipped } from "./shipping";

const hooks = webhooks({ maxFailures: 5, publishers: [notifier] });

export default app({
  shipped,
  ship,
  hooks,
  shipping: follow(shipped, { roles: [notifier] }, toWebhooks(hooks.events.publish, (event) => event.data)),
});
```

`toWebhooks(publish, map)` calls a mounted [Webhooks app](https://docs.bijection.com/integrations/native-webhooks.md)'s
`events.publish` with the event's key, as the follower's `notifier` role,
which the mount allows because the app passes it in `publishers`. `toStream(target, map)` submits one
task per event to a stream target with the follower's `once`. Every
delivery marks the event handled in the handler's commit, so no event is
handled twice.

## Replay

`<path>.events.start` replays the event table's change log, a page per
delivery, from a retained version or from the earliest retained change
(`null`). Roles in the follower's `allow` may call it; without `allow`, only
your own code may. A replay whose range the table's `history` no longer
holds starts again from the whole table, and the marks skip what was
already handled.

## Follow one family member

Declare the event with `per: family`. Publish from a frame under one
member, and the event is that member's fact. The live reaction runs under
each event's member, so its mark and the handler's writes are that
member's.

## Pin a registry schema

For a payload held in a schema registry, scaffold the fields into an
authored TypeScript module, pinned to an exact version:

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
bijection events import --registry https://registry.example.com \
  --subject orders-shipped-value --version 4 --format avro \
  --out bijection/order-shipped.ts

bijection events check bijection/order-shipped.ts
```

The scaffold carries the registry identity, schema id and digest beside
the fields. Review it and commit it. `check` resolves the pin again and
compares its id and digest. `--connection <file>` supplies registry
settings and credential environment names. `--format` also takes
`json-schema` and `protobuf`; select a Protobuf message with `--message`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.