# Signals and events

> Reference for declaring and publishing a plugin signal, and for reading the persisted event log from a plugin.

A signal is a typed event. A plugin declares the signals it publishes, publishes them with `context.events.emit`, and can read the event log, which holds the signals of adapters and plugins. For adapter signals, see [signals](/docs/adapter-sdk/signals).

## Declare and publish a signal

Declare the signal in `setup` with an `id`, a Zod `payload` schema, and a `type` of `info` (the default), `success`, `warning`, or `error`:

```ts
registry.signal({
  id: "ticked",
  payload: z.object({ n: z.number() }),
  type: "info",
});
```

Publish it from a job or a procedure. The optional third argument is the ID of the service that the event is about:

```ts
await context.events.emit("ticked", { n: 7 });
```

DSUI validates the payload against the schema, adds the plugin's ID to the signal as `<plugin id>.ticked`, and stores the event. It then queues the jobs that subscribe to it with `onSignals`. Publishing an undeclared signal throws `Undeclared plugin signal`.

In the event log, a plugin event has `sourceType` set to `plugin` and `sourceId` set to the plugin's ID. See [HTTP API](/docs/reference/http-api#read-events).

When a person's request causes an event, DSUI records their principal ID as `actor`. This covers events a plugin publishes while handling someone's request, and the events of a service action someone runs. Background jobs and local single-user mode record no actor.

### Signals about services

If the plugin emits a signal about individual services, as Service health does when a service goes down, declare it with `scope: "service"`:

```ts
registry.signal({
  id: "service-unavailable",
  payload: z.object({ summary: z.string() }),
  type: "error",
  scope: "service",
});
```

DSUI then lists it with every service's own signals in `context.services.signals(serviceId)`, so signal pickers, such as workflow editors, can offer it before it has ever fired.
Emit it with the service's ID.
A signal without `scope`, or with `scope: "global"`, is not listed for services.

## Actions people run

Every service action someone runs leaves one event. When the adapter publishes its own signal for the action, such as a failed query, that event names the person as `actor`. Otherwise DSUI records `dsui.action-executed`, with `sourceType` set to `host`, the `actor`, and this payload:

| Field | Meaning |
| --- | --- |
| `action` | The action ID |
| `adapter` | The adapter ID of the service |
| `status` | `success` or `error` |
| `message` | Why it failed, when `status` is `error` |
| `plugin` | The plugin that ran the action on the person's behalf, when one did |

It never contains the action's input, and its type is `info` even when the action failed, so it does not trigger alerts.

## Read events

`context.events.read({ cursor, limit })` returns persisted events in the order they were added, as `{ items, cursor, hasMore }`.

```ts
const page = await context.events.read?.({ limit: 50 });
```

- **`limit`**: from 1 to 500
- **`cursor`**: where to continue. Omit it to replay the available history, or pass `"latest"` to start at the current end
- **`hasMore`**: whether more events follow

To process events once, save the returned `cursor` in plugin storage together with your results, and ignore events that you already handled by their `id`. A page can be empty and still move the cursor forward, because DSUI hides events for services that the caller can't see. Events that belong to no service are visible only to administrators and background jobs. A cursor belongs to one DSUI database, and it isn't a timestamp.

`events.read` is optional. A host that lacks it leaves it undefined, so check that it exists before you rely on it.
