# 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.

## noclick.Room

```python
class Room
```

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

Usage:

```python
room = noclick.Room(f"workflow:{workflow_id}")
print([m.user["name"] for m in room.members() if m.user])
```

| Attribute | Type | Description |
| --- | --- | --- |
| `name` | `str` |  |

### noclick.Room

```python
noclick.Room(name, *, client=None)
```

Parameters:

- `name` (`str`, required)
- `client` (`Client | None`, default `None`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

### Room.members

```python
room.members() -> list[RoomMember]
```

Who is in the room now, each as the room shows them.

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

Usage:

```python
for member in noclick.Room(f"workflow:{workflow_id}").members():
    print(member.user and member.user["name"], member.data.get("selection"))
```

### Room.history

```python
room.history(*, since=None) -> dict[str, Any]
```

The room's recent events (at most its last 500 of the last ten minutes), after `since` when it names one
still kept.

Parameters:

- `since` (`str | None`, default `None`)

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

Usage:

```python
found = noclick.Room(f"workflow:{workflow_id}").history(since=last_id)
for event in found["events"]:
    print(event.event, event.data)
```

### Room.publish

```python
room.publish(event, data=None) -> str
```

Publishes `event` (lower case: letters, digits, `_ . : -`) with `data` (at most 64 KB as JSON) to
everyone in the room, named as from you.

Parameters:

- `event` (`str`, required)
- `data` (`Any`, default `None`)

Returns: `str` The event's id in the room.

Usage:

```python
noclick.Room(f"workflow:{workflow_id}").publish("review.ready", {"step": "draft"})
```

### Room.ask

```python
room.ask(name, args=None, *, timeout=10.0, collect_ms=0) -> dict[str, Any]
```

Asks the room's pages that answer `name` (the TypeScript SDK's `room.answer`) and waits for an
answer, at most `timeout` seconds (25 at most). With `collect_ms`, waits that long after the first answer
for the others'.

Parameters:

- `name` (`str`, required)
- `args` (`dict[str, Any] | None`, default `None`)
- `timeout` (`float`, default `10.0`)
- `collect_ms` (`int`, default `0`)

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

Usage:

```python
reply = noclick.Room(f"workflow:{workflow_id}").ask("canvas.get_selected", timeout=5)
```

### Room.token

```python
room.token(*, origin=None) -> RoomToken
```

A single-use token for a page to join the room with (`Room.join(name, {token})` in TypeScript): pass
`url` and `token` on as they are. `origin` is the page's (`location.origin`); the socket must come from
it.

Parameters:

- `origin` (`str | None`, default `None`)

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

Usage:

```python
token = noclick.Room(f"workflow:{workflow_id}").token(origin="https://app.example.com")
return {"url": token.url, "token": token.token}
```

## noclick.RoomEvent

```python
class RoomEvent
```

What was published in a room: its `id` (what `history(since=)` reads after), its name, its data, and who
published it (`from_`; None for NoClick's own).

| Attribute | Type | Description |
| --- | --- | --- |
| `id` | `str` |  |
| `event` | `str` |  |
| `data` | `Any` |  |
| `from_` | `dict[str, Any] | None` |  |
| `raw` | `dict[str, Any]` | The response JSON exactly as the API sent it. |

## noclick.RoomMember

```python
class 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` | `str` |  |
| `user` | `dict[str, Any] | None` |  |
| `color` | `str | None` |  |
| `data` | `dict[str, Any]` |  |
| `server` | `bool` |  |
| `joined_at` | `datetime | None` |  |
| `raw` | `dict[str, Any]` | The response JSON exactly as the API sent it. |

## noclick.RoomToken

```python
class RoomToken
```

A single-use token for a room's socket: hand `url` and `token` to the page that joins, within a minute.

| Attribute | Type | Description |
| --- | --- | --- |
| `url` | `str` |  |
| `token` | `str` |  |
| `access` | `str` |  |
| `expires_at` | `datetime | None` |  |
| `connection_ends_at` | `datetime | None` |  |
| `raw` | `dict[str, Any]` | The response JSON exactly as the API sent it. |
