Skip to content

API reference

Room

Bases: AsyncContextManagerMixin

doc property

Returns:

Type Description
Doc

The room's shared document.

id property

Returns:

Type Description
str

The room ID.

task_group property

Returns:

Type Description
TaskGroup

The room's task group, that can be used to launch background tasks.

__init__(id)

Creates a new room in which clients with the same ID will be connected.

Parameters:

Name Type Description Default
id str

The room ID.

required

run(*, task_status=TASK_STATUS_IGNORED) async

The main background task which is responsible for forwarding every update from a client to all other clients in the room.

Parameters:

Name Type Description Default
task_status TaskStatus[None]

The task status that is set when the task has started.

TASK_STATUS_IGNORED

serve(client, *, task_status=TASK_STATUS_IGNORED) async

The handler for a client which is responsible for the connection handshake and for applying the client updates to the room's shared document.

Parameters:

Name Type Description Default
client Channel

The client making the connection.

required
task_status TaskStatus[None]

The task status that is set when the task has started.

TASK_STATUS_IGNORED

bind(wire, room_factory=Room, **kwargs)

Creates a server using a wire, and its specific arguments. The server must always be used with an async context manager, for instance:

async with bind("websocket", host="localhost", port=8000) as server:
    ...

Parameters:

Name Type Description Default
wire str

The wire used to accept connections.

required
room_factory Callable[[str], Room]

An optional callable used to create a room.

Room
kwargs Any

The arguments that are specific to the wire.

{}

Returns:

Type Description
ServerWire

The created server.

connect(wire, *, id='', doc=None, **kwargs)

Creates a client using a wire, and its specific arguments. The client must always be used with an async context manager, for instance:

async with connect("websocket", host="localhost", port=8000) as client:
    ...

Parameters:

Name Type Description Default
wire str

The wire used to connect.

required
id str

The ID of the room to connect to in the server.

''
doc Doc | None

An optional external shared document (or a new one will be created).

None
kwargs Any

The arguments that are specific to the wire.

{}

Returns:

Type Description
ClientWire

The created client.