---
title: Listen to events
description: Run a handler in one luna of your type for each event, choose which luna by its key, and know how order and retries work.
related:
  - /events/emit-events
  - /events/react-to-table-changes
  - /sdk/define-event
---

An emitted event does nothing until a luna type listens to it. A type can have millions of lunas, so the listener must also say which one of them hears each event.

## Listen to an event

The `on` option of [`defineLuna`](/sdk/define-luna) receives `listen` and returns a list of listens. Each [`listen`](/sdk/define-event) takes an event, a `key` function and a handler.

```ts luna/lunas/user.ts {9-13}
import { defineLuna, t, table } from "luna";
import { CardAssigned } from "../events.ts";

export const User = defineLuna({
  name: "User",
  key: { id: t.string() },
  schema: { inbox: table({ n: t.id(), board: t.string(), card: t.int() }) },
  on: (listen) => [
    listen(CardAssigned, { key: (e) => e.user }, (ctx, e, from) => {
      const board = String(from.key.board);
      ctx.db.inbox.insert({ board, card: e.card });
    }),
  ],
  handlers: (h) => ({
    inbox: h({ query: true }, (ctx) => ctx.db.inbox.find({ orderBy: "n" })),
  }),
});
```

The handler runs in the `User` luna whose key the `key` function returns. It gets three arguments:

| Argument | Holds |
| --- | --- |
| `ctx` | The listening luna's context, as in any handler that writes |
| `e` | The event's payload |
| `from` | The emitting luna: its type in `from.type` and its key in `from.key` |

The listening luna does not need to exist first. The event wakes it, and its writes are stored like any other call's.

## Pick which luna hears each event

The `key` function returns the listening luna's key. It may only name fields. Code that computes with a field throws when the luna type is defined.

This key builds a string, so it fails:

```ts luna/lunas/user.ts
listen(CardAssigned, { key: (e) => `user-${e.user}` }, handler)
```

This key names the field, so it works:

```ts luna/lunas/user.ts
listen(CardAssigned, { key: (e) => e.user }, handler)
```

| The key names | The luna that hears the event |
| --- | --- |
| A field of the payload, `(e) => e.user` | The one keyed by that field's value |
| A key field of the emitter, `(e, from) => from.board` | The one keyed by the emitting luna's field |
| A constant, `() => "all"` | Always the same one |
| `day(e.at)`, for a field in milliseconds | The one keyed by that UTC day, as `YYYY-MM-DD` |
| `hash(16)` | One of 16, by a hash of the emitting luna's key |

`day` and `hash` are imported from `"luna"`. `hash` takes 1 to 1,024 buckets. A listening type with several key fields returns an object with each of them, such as `{ board: from.board, card: e.card }`.

## Reach more than one luna

Each listen sends each event to one luna of the listening type. No listen reaches every luna of a type at once.

To gather every event in one place, listen from a luna type with no `key`, and pass `{}` as the options. That type has a single luna, and it hears every event. To reach other lunas from there, send each one a message with `ctx.send`, as [Call another luna](/lunas/call-another-luna) shows.

## Order, retries and repeats

Events from one emitting luna reach a listener in the order they were emitted. Events from two different emitters have no order between them.

Luna hands a listener up to 500 events at a time. The handler runs once for each, and all of their writes are stored together. A batch whose writes were stored never runs again.

If the handler throws for any event of a batch, the writes of the whole batch are undone. Luna tries the batch again later, with longer waits each time, until it succeeds. `ctx.fail` in a listener counts as a throw.

:::note
Later events from the same emitter wait behind a batch that keeps failing. Throw only for a reason that time fixes, such as another luna that is not reachable. A request to the outside world in a listener runs again each time its batch is tried.
:::

## Listeners added later

A deploy that adds a listen sends it the events each emitting luna still keeps, in the background. How long they are kept is set by the event's retention, which [Emit events](/events/emit-events) covers.

Changing an existing listen, such as its `key`, applies to events from then on. Events it already handled are not sent again.

In Python, `listen` takes `key` and `handler` by name. [Write lunas in Python](/lunas/write-lunas-in-python) shows the differences.

## Next steps

- [React to table changes](/events/react-to-table-changes) listens to rows instead of events.
- [Query across lunas](/data/query-across-lunas) keeps events in an index that handlers and apps can query.
- [Call another luna](/lunas/call-another-luna) sends messages onward from a listener.
