Rooms

Open in

The Rooms interface manages the lifecycle of chat rooms.

Access it via the rooms property on a ChatClient instance.

JavaScript

1

const rooms = chatClient.rooms;

Create or retrieve a room

rooms.get(name: string, options?: RoomOptions): Promise<Room>

Create or retrieve a Room instance. Optionally provide custom configuration to the room.

Call release() when the Room is no longer needed. If a call to get() is made for a room that is currently being released, then the promise will only resolve when the release operation is complete.

If a call to get() is made, followed by a subsequent call to release() before the promise resolves, then the promise will reject with an error.

JavaScript

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

const options = {
  typing: {
    heartbeatThrottleMs: 5000,
  },
  presence: {
    enableEvents: false,
  },
  occupancy: {
    enableEvents: true,
  },
  messages: {
    rawMessageReactions: false,
    defaultMessageReactionType: 'unique',
  },
};

const room = await chatClient.rooms.get('basketball-stream', options);

// When finished with the room
await chatClient.rooms.release('basketball-stream');

Parameters

The get() method takes the following parameters:

namerequiredString
The unique identifier of the room.
optionsoptionalRoomOptions
Configuration for the room features.

Returns

Promise<Room>

Returns a promise. The promise is fulfilled with the new or existing Room instance, or rejected with an ErrorInfo object. The promise is also rejected if the room already exists with different options, or if release() is called before get() resolves.

Release a room

rooms.release(name: string): Promise<void>

Releases a room, freeing its resources and detaching it from Ably.

After release, the room object is no longer usable. To use the room again, call get() to create a new instance.

JavaScript

1

await chatClient.rooms.release('my-room');

Parameters

The release() method takes the following parameters:

namerequiredString
The unique identifier of the room to release.

Returns

Promise<void>

Returns a promise. The promise is fulfilled when the room is fully released, or rejected with an ErrorInfo object.

Dispose of all rooms

rooms.dispose(): Promise<void>

Releases all rooms, freeing their resources and detaching them from Ably. After calling this method, any existing room objects obtained from this instance are no longer usable.

JavaScript

1

2

// Release all rooms at once
await chatClient.rooms.dispose();

Returns

Promise<void>

Returns a promise. The promise is fulfilled when all rooms have been released, or rejected with an ErrorInfo object.