# Room

> Room: a realtime room's server side. Publish to the pages in it, see who's there, ask them, and mint a token for a page to join (the TypeScript SDK's `Room.join`).

Rooms are named by what they're about: `workflow:{id}` (everyone who may open the workflow) and `owner` (your own
open tabs, as a signed-in person). Publishing and asking need edit access to a workflow's room; an event you publish
names you (`from`), where NoClick's own events name nobody.

## Room

```typescript
class Room
```

A room by its name (`workflow:{id}`, `owner`). Nothing is read until you call a method.

| Attribute | Type | Description |
| --- | --- | --- |
| `name` | `string` |  |

### Room.members

```typescript
static members(
  name: string,
  options?: { client?: NoClick },
): Promise<RoomMember[]>
```

Who is in the room now, read through the API (no socket needed).

Parameters:

- `name` (`string`, required)
- `options.client` (`NoClick`)

Returns: Every member: the pages in the room, and the members NoClick holds (`server`).

### Room.history

```typescript
history(
  options?: { since?: string | null; client?: NoClick },
): Promise<{ events: RoomEvent[]; reset: boolean }>
```

The room's recent events (at most its last 500 of the last ten minutes), after `since` when it names one still
kept; `reset` when it doesn't (read what you show again).

Parameters:

- `options.since` (`string | null`)
- `options.client` (`NoClick`)

Returns: `{"events": [RoomEvent, ...], "reset": bool}`; `reset` True when `since` was too old, so read what
you show again.

### Room.publish

```typescript
static publish(
  name: string,
  event: string,
  data?: unknown,
  options?: { client?: NoClick },
): Promise<string>
```

Publishes `event` with `data` (at most 64 KB as JSON) to everyone in the room, named as from you (needs edit access
to a workflow's room); its id in the room. From a server: a page in the room broadcasts instead.

Parameters:

- `event` (`string`, required)
- `data` (`unknown`, default `null`)
- `name` (`string`, required)
- `options.client` (`NoClick`)

Returns: The event's id in the room.

### Room.ask

```typescript
static ask(
  name: string,
  ask: string,
  args?: JsonObject,
  options?: { client?: NoClick; timeoutS?: number; collectMs?: number },
): Promise<JsonObject>
```

Asks the room's pages that answer `ask` (`room.answer`) and waits for an answer, at most `timeoutS` (25 at most);
with `collectMs`, that long after the first for the others'. `{ answer }`, `{ answers }`, or `{ error }`
(`no_viewer`, `timeout`). Needs edit access to a workflow's room.

Parameters:

- `name` (`string`, required)
- `args` (`JsonObject`)
- `ask` (`string`, required)
- `options.collectMs` (`number`, default `0`)
- `options.client` (`NoClick`)
- `options.timeoutS` (`number`, default `10`)

Returns: `{"answer": {...}}` (or `{"answers": [...]}` collecting), each naming the member that gave it, or
`{"error": "no_viewer" | "timeout"}`.

Types:

```typescript
type JsonObject = { [key: string]: unknown };
```

### Room.token

```typescript
static token(name: string, options?: RoomTokenOptions): Promise<RoomToken>
```

A single-use token for the room's socket, for a page to open with `Room.join(name, { token })`.

Parameters:

- `name` (`string`, required)
- `options.origin` (`string`): The page's origin the socket will come from (a browser sends its own).
- `options.client` (`NoClick`)

Returns: The token, the socket's `url` (carrying its ticket) and what it lets the page do (`access`).

### Room.access

```typescript
access: RoomAccess
```

What your token lets you do here (renewed in place, so it follows an access change).

Types:

```typescript
/** What a token lets you do in a room: edit or view a workflow's, or your own tabs (`own`). */
type RoomAccess = "edit" | "view" | "own";
```

### Room.me

```typescript
me: RoomMember | null
```

Your own member, once the room let you in.

### Room.status

```typescript
status: RoomStatus
```

Types:

```typescript
/** Where the room's socket stands: `connecting`, `live`, `reconnecting` (it will on its own), or `stopped`. */
type RoomStatus = "connecting" | "live" | "reconnecting" | "stopped";
```

### Room.runs

```typescript
runs: RoomRun[]
```

The runs in flight on a workflow's room as you last joined.

Types:

```typescript
/** A run in flight on a workflow's room, as a page joining mid-run is told it. */
interface RoomRun {
  executionId: string;
  nodeStates: Record<string, string>;
  nodeOutputs: Record<string, unknown>;
}
```

### Room.presence

```typescript
presence: { set: (data: JsonObject) => void; list: () => RoomMember[] }
```

Your presence: `set` merges what you say of yourself; `list` is everyone here now, you included.

Types:

```typescript
type JsonObject = { [key: string]: unknown };
```

### Room.on

```typescript
on(event: "presence", handler: (members: RoomMember[]) => void): () => void
on(event: "event", handler: (event: RoomEvent) => void): () => void
on(event: "status", handler: (status: RoomStatus) => void): () => void
on(event: "access", handler: (access: RoomAccess) => void): () => void
on(event: string, handler: BroadcastHandler): () => void
```

Everyone's presence as it changes (`members`, you included).
The server's events.
What your token lets you do, each time it changes (a renewal after an access change).
What another member broadcast under this name.

Parameters:

- `event` (`"presence"`, required)
- `handler` (`(members: RoomMember[]) => void`, required)

### Room.broadcast

```typescript
broadcast(event: string, data?: unknown): void
```

Tells the room's other members something (relayed only where the room's kind allows it).

Parameters:

- `event` (`string`, required)
- `data` (`unknown`, default `null`)

### Room.answer

```typescript
answer(name: string, handler: (args: JsonObject) => unknown): () => void
```

Answers what the server asks this page under `name` (the first answer, or each with collecting, wins).

Parameters:

- `name` (`string`, required)
- `handler` (`(args: JsonObject) => unknown`, required)

Types:

```typescript
type JsonObject = { [key: string]: unknown };
```

### Room.scope

```typescript
scope(workflowId: string | null): void
```

On your own room: hear only events for one workflow (and those for none); null hears every one.

Parameters:

- `workflowId` (`string | null`, required)

### Room.stop

```typescript
stop(executionId: string): void
```

On a workflow's room: stop a run (if you may edit, or it's yours).

Parameters:

- `executionId` (`string`, required)

### Room.leave

```typescript
leave(): void
```

Leaves the room: the socket closes and nothing more is heard.

### Room.join

```typescript
static join(name: string, options?: RoomJoinOptions): Promise<Room>
```

Joins the room: resolves once you're in (with `members` as they are), and stays in, reconnecting by itself, until
`leave` (or `signal`). Throws what trying again can't fix: the token call refused (a room you can't open, a key
without the scope) or the room refusing two fresh tokens in a row (`RoomRefused`).

Parameters:

- `name` (`string`, required)
- `options.presence` (`JsonObject`): What you say of yourself as you join (merged; null removes a key). At most 1 KB as JSON.
- `options.token` (`() => Promise<{ url: string; token: string }>`): Where a token comes from, when not this client: your server's endpoint passing `url` and `token` on.
- `options.onStatus` (`(status: RoomStatus) => void`)
- `options.signal` (`AbortSignal`): Stops the room (as `leave`).
- `options.WebSocket` (`typeof WebSocket`): A WebSocket class, where the runtime has none of its own (Node before 22).
- `options.client` (`NoClick`)
- `options.origin` (`string`): The page's origin the socket will come from (a browser sends its own).

Types:

```typescript
type JsonObject = { [key: string]: unknown };

/** Where the room's socket stands: `connecting`, `live`, `reconnecting` (it will on its own), or `stopped`. */
type RoomStatus = "connecting" | "live" | "reconnecting" | "stopped";
```

## RoomEvent

```typescript
interface RoomEvent
```

What the server published: NoClick's own (`from` null), or a member's through the API.

| Attribute | Type | Description |
| --- | --- | --- |
| `id` | `string` | Its place in the room (what a reconnect resumes after, and `history({ since })` reads after). |
| `event` | `string` |  |
| `data` | `unknown` |  |
| `from` | `{ id: string; name: string } | null` |  |
| `raw` | `JsonObject` | The response JSON exactly as the API sent it. |

Types:

```typescript
type JsonObject = { [key: string]: unknown };
```

## RoomMember

```typescript
interface RoomMember
```

One member of a room: a page (`user` who they are, `color` the room gave them, `data` what they say of
themselves) or one NoClick holds (`server`: an agent's sandbox at work, `user` None).

| Attribute | Type | Description |
| --- | --- | --- |
| `id` | `string` | This connection's id in the room: one person's two tabs are two members. |
| `user` | `RoomUser | null` |  |
| `color` | `string | null` | The colour the room gave them; null for a member NoClick holds. |
| `data` | `JsonObject` | What they say of themselves (`presence.set`). |
| `server` | `boolean` | One NoClick holds, such as an agent's sandbox at work. |
| `joinedAt` | `Date | null` |  |
| `raw` | `JsonObject` | The response JSON exactly as the API sent it. |

Types:

```typescript
/** Who a member is, as their token says: null for one NoClick holds (an agent's sandbox). */
interface RoomUser {
  id: string;
  name: string;
  avatar: string | null;
  /** NoClick's staff viewing the account as its owner. */
  staff: boolean;
}

type JsonObject = { [key: string]: unknown };
```

## RoomToken

```typescript
interface RoomToken
```

A single-use token for a room's socket (`POST /v1/rooms/{room}/tokens`).

| Attribute | Type | Description |
| --- | --- | --- |
| `url` | `string` |  |
| `token` | `string` |  |
| `access` | `RoomAccess` |  |
| `expiresAt` | `Date | null` |  |
| `connectionEndsAt` | `Date | null` |  |
| `raw` | `JsonObject` | The response JSON exactly as the API sent it. |

Types:

```typescript
/** What a token lets you do in a room: edit or view a workflow's, or your own tabs (`own`). */
type RoomAccess = "edit" | "view" | "own";

type JsonObject = { [key: string]: unknown };
```
