# molted listen

Source: https://molted.email/docs/cli/listen

Connect to the SSE event stream — persistent event loop for agents

## Agent context

The listen command connects to the SSE event stream and acts as a persistent event loop for agents. It turns the CLI from a one-shot command tool into an agent runtime that reacts to events in real-time.

### Print a one-line summary of each event (to stderr)

```bash
molted listen
```

### Pipe to a long-running agent process (restarted if it exits)

```bash
molted listen --pipe "node my-agent.js"
```

### `--pipe` and `--json` both write one JSON line per event

```bash
{ "event": "<type>", "id": "<eventId>", "data": { ...payload }, "timestamp": "..." }
```

### Filter events

```bash
molted listen --events "inbound.*"
molted listen --events "delivery.bounced,delivery.complained" --pipe "./bounce-agent.py"
```

### JSON mode for shell piping

```bash
molted listen --json | jq '.event'
```

### Works with pm2/systemd for persistent agent runtimes

```bash
pm2 start "molted listen --pipe ./agent.js --events 'inbound.*'" --name my-agent
```

With `--since`, a resumed connection replays missed events first (including any matching a wildcard filter). If the server can't guarantee it caught everything -- a large burst during replay, or a backlog too big to page through -- it prints a "replay gap" warning to stderr instead of silently dropping the difference; the stream itself keeps running.

Related: [`molted events`](https://molted.email/docs/cli/events.md)

## Commands

### molted listen

Connect to the SSE event stream — persistent event loop for agents

```bash
molted listen [options]
```

| Option | Description | Default |
| - | - | - |
| `--pipe <command>` | Long-running handler process (receives JSON lines on stdin) | |
| `--events <types>` | Comma-separated event types to filter (supports wildcards: "delivery.\*") | |
| `--json` | Output one JSON line per event to stdout (for shell piping) | |
| `--concurrency <n>` | Max events being dispatched at once | `1` |
| `--retries <n>` | Retries for a failed write to the `--pipe` handler before the event is skipped | `3` |
| `--since <eventId>` | Resume from a specific event ID | |

See [CLI overview](https://molted.email/docs/cli.md) for global options, credentials and output format.
