Rooms

Rooms are used to organize and logically separate your users and chat messages into ‘rooms’. They are the entry object into using chat and provide access to all other chat features, such as messages, online status and typing indicators. A room can represent a 1:1 chat between an agent and a customer, a private message between two users in a chat application, a large group conversation, or the chat section of a livestream with thousands of users.

You also control which features are enabled in a room when you create one. For instance, you might only want to utilize typing indicators in direct messages between users.

Users send messages to a room and subscribe to the room in order to receive messages. Other features, such as indicating which users are online, or which users are typing are configured as part of a room’s options.

A room is created, or an existing one is retrieved from the rooms collection using the rooms.get() method:

Select...
const room = chatClient.rooms.get('basketball-stream', RoomOptionsDefaults);
Copied!

When you create or retrieve a room using rooms.get(), you need to choose which additional chat features you want enabled for that room. This is configured by passing a RoomOptions object as the second argument. In addition to setting which features are enabled, RoomOptions also configures the properties of the associated features, such as the timeout period for typing indicators.

Select...
const presence = {enter: false}; const reactions = {}; const occupancy = {}; const typing = {timeoutMs: 5000}; const room = chatClient.rooms.get('basketball-stream', {presence, reactions, typing, occupancy});
Copied!

You can also use the RoomOptionsDefaults property for each RoomOption to configure whether the default settings should be used:

Select...
const room = chatClient.rooms.get('basketball-stream', {presence: RoomOptionsDefaults.presence});
Copied!

Or configure each feature using your own values:

Select...
const room = chatClient.rooms.get('basketball-stream', {typing: {timeoutMs: 5000}});
Copied!

Enable each feature using its associated option. The details of the options available to each feature are documented on their respective pages:

Feature RoomOption Default settings
Presence presence RoomOptionsDefaults.presence
Occupancy occupancy RoomOptionsDefaults.occupancy
Typing indicators typing RoomOptionsDefaults.typing
Room reactions reactions RoomOptionsDefaults.reactions

Releasing a room allows the underlying resources to be garbage collected or released.

Releasing a room may be optional for many applications. If you have multiple transient rooms, such as in the case of a 1:1 support chat, then it is may be more beneficial.

Once rooms.release() has been called, the room will be unusable and a new instance will need to be created using rooms.get() if you want to reuse it.

Select...
await rooms.release('basketball-stream');
Copied!

As soon as a client is attached to a room, Ably will begin streaming messages and events to them, regardless of whether or not they have registered any listeners to receive those messages and events.

Once a reference to a room has been created using rooms.get(), clients attach to it in order to ensure it is created in the Ably system.

Use the attach() method on a room to attach to it:

Select...
await room.attach();
Copied!

Use the detach() method on a room to detach from it and stop receiving messages and events:

Select...
await room.detach();
Copied!

If a client detaches from a room without de-registering any of their listeners, they can subsequently re-attach at a later point and their listeners will continue to receive messages and events.

Monitoring the status of a room enables you to track its lifecycle and react accordingly.

A room can have any of the following statuses:

Status Description
initializing The library is initializing the room.
initialized The room has been initialized, but no attach has been attempted yet.
attaching An attach has been initiated by sending a request to Ably. This is a transient status and will be followed either by a transition to attached, suspended, or failed.
attached An attach has succeeded. In the attached status a client can publish and subscribe to messages, and enter the presence set.
detaching A detach has been initiated on the attached room by sending a request to Ably. This is a transient status and will be followed either by a transition to detached or failed.
detached The room has been detached by the client.
suspended The room, having previously been attached, has lost continuity. This is normally due to the client being disconnected from Ably for more than two minutes. The client will automatically attempt to reattach as soon as connectivity is restored.
failed An indefinite failure condition. This status is entered if an error has been received from Ably, such as an attempt to attach without the necessary access rights.

Use the current property to check which status a room is currently in:

Select...
const currentStatus = room.status.current
Copied!

You can also subscribe to room status updates by registering a listener. An event will be emitted whenever the status of the room changes.

Use the room.status.onChange() method in a room to register a listener for status change updates:

Select...
const { off } = room.onStatusChange((change) => console.log(change));
Copied!

To remove the room status listener, call the provided off() function:

Select...
off();
Copied!

Use the room.status.offAll() method to remove all room status listeners in a room:

Select...
room.status.offAll();
Copied!
Create or retrieve a room
v2.0