# trigger.room

Declares a room and runs when a client joined to it sends an event — the server half of a chat, a live board, a match

# trigger.room

```
trigger.room --room TEXT [--event TEXT] [--every DURATION] [--max-inputs N] [--session-idle DURATION] [--state TEXT…] [--auth none|jwt|hmac|api_key] [--credential TEXT] [--role TEXT…] → room
```

Declares a room and runs when a client joined to it sends an event — the server half of a chat, a live board, a match. `--room lobby` (an exact name) is a fixed room, there while this trigger is active; `--room "match-:id"` or `"zone-*"` (a pattern) declares opened rooms, which exist only after [`ws.room.open`](/nodes/ws/ws.room.open.md) and until [`ws.room.close`](/nodes/ws/ws.room.close.md) or their `--idle`; joining one that is not open closes with 4404. `--room` is required and `*` alone is not a room. `--event` picks one event name (empty = any client event). `--auth` / `--credential` / `--role` are the room's join rule, read as on [`trigger.webhook`](/nodes/trigger/trigger.webhook.md) and checked once, at connect (refused: 4401); every active trigger.room that can match the same room must declare the same rule, or activation refuses. Answers one key, `room`: `{ room_id, session_id, event, payload, auth }` — `auth` is what the rule proved at connect (or `{ sub }` from a ticket). Answer with [`ws.message.send`](/nodes/ws/ws.message.send.md) or [`ws.state.put`](/nodes/ws/ws.state.put.md) / [`ws.state.update`](/nodes/ws/ws.state.update.md) / [`ws.state.delete`](/nodes/ws/ws.state.delete.md); a [`web.response.send`](/nodes/web/web.response.send.md) here answers nobody. The server raises `$connect` and `$disconnect` (payload `{ reason }`: left, dropped, closed, room_closed, expired, flood, timeout) — and in a room opened with `--hold`, `$away` when a connection drops and `$back` when it resumes — on the room's one queue: a room handles one thing at a time, in arrival order across all its sessions, so a pipeline that reads and writes back needs no lock — only a trigger naming one (`--event $connect`) receives it, and clients cannot send `$` events. `--event $tick --every 50ms` (16ms–1s) makes a tick room: every client event waits for the next tick, and this trigger runs once per tick with `room: { tick, dt_ms, inputs: [ { session, event, payload, auth } ] }` in arrival order — at most `--max-inputs` (default 30) per session, the rest dropped and counted; ticks never overlap (one that outlasts `--every` delays the next and counts a tick_overrun), an empty tick still runs, and state changes go out once, after the tick. `--state /pos` (repeatable; `/` = all) adds `state: { pos: { … } }` to `room`: the room's state at those paths, read just before the run — only what is named is copied. The client is a plain `WebSocket` to `/ws/{owner}/{project}/rooms/{room}` (`?ticket=` for a private room) naming the protocol `zebflow.rooms.v1`, receiving `joined`, `patch`, `event` and `resync` (help topic `pipeline/rooms-protocol`).

## Flags

- `--room` (required): A fixed room (lobby) or a pattern of opened rooms (match-:id, zone-*). Required; * alone is not a room.
- `--event`: Event name to match. Omit to match any event from connected clients; $tick makes a tick room.
- `--every`: With --event $tick: how often the room ticks, 16ms to 1s.
- `--max-inputs`: With --event $tick: per session, the most inputs that wait for one tick (default 30); extras are dropped and counted.
- `--session-idle`: Close a session that sends no event for this long, 5s to 24h, with $disconnect { reason: "idle" }. Absent is no limit.
- `--state`: A path of the room's state each run gets as input.room.state, repeated (--state /pos --state /hp); / is the whole state.
- `--auth`: How joining the room is guarded: none (default), jwt, hmac, api_key. Anything but none needs --credential.
- `--credential`: The credential (its id) that verifies --auth: a JWT signing key, an hmac signature check (help pipeline/examples/webhook-signatures) or an API key.
- `--role`: A role allowed in, repeated (`--role editor --role admin`): one entry of the JWT `roles` claim must match. None: any authenticated caller.

## Examples

### Chat message in

```
| trigger.room --room lobby --event chat.send
```

Then `| ws.message.send --event chat.message --body "{{ { from: input.room.session_id, text: input.room.payload.text } }}"`.

It answers:

```json
{
  "room": {
    "room_id": "lobby",
    "session_id": "s_8f2",
    "event": "chat.send",
    "payload": {
      "text": "hello"
    },
    "auth": null
  }
}
```

### A match ticking 20 times a second

```
| trigger.room --room "match-:id" --event $tick --every 50ms
```

Every client event of the room waits for the tick; resolve them in one run, in order, and every client gets one result.

It answers:

```json
{
  "room": {
    "room_id": "match-1",
    "event": "$tick",
    "tick": 412,
    "dt_ms": 50,
    "inputs": [
      {
        "session": "s_8f2",
        "event": "move",
        "payload": {
          "dx": 1
        },
        "auth": {
          "sub": "u-17"
        }
      }
    ]
  }
}
```

### A tick that reads where everyone stands

```
| trigger.room --room world --event $tick --every 100ms --state /pos
```

Refuse a move onto a taken tile: input.room.state.pos holds every player's tile as the tick starts.

It answers:

```json
{
  "room": {
    "room_id": "world",
    "event": "$tick",
    "tick": 88,
    "dt_ms": 100,
    "inputs": [
      {
        "session": "s_8f2",
        "event": "move",
        "payload": {
          "x": 4,
          "y": 7
        },
        "auth": null
      }
    ],
    "state": {
      "pos": {
        "s_8f2": {
          "x": 4,
          "y": 6
        },
        "s_1c9": {
          "x": 4,
          "y": 7
        }
      }
    }
  }
}
```

## Used in

- [Forum with Real-Time Chat](/examples/forum-with-chat.md)
- [Real-Time Game (WebSocket State Sync)](/examples/realtime-game.md)
