Disposes all rooms that are currently in the rooms map. This method releases all rooms concurrently and clears the rooms map.
A promise that resolves when all rooms have been released.
Gets a room reference by its unique identifier.
Creates a new room instance or returns an existing one. The Rooms class ensures
only one instance exists per room name. Always call release() when the room
is no longer needed to free resources.
Note:
get is called during a release, it waits for release to complete.release is called before get resolves, the promise rejects.The unique identifier of the room
Optionaloptions: RoomOptionsOptional configuration for the room features
Promise resolving to the Room instance, or rejecting with:
import * as Ably from 'ably';
import { ChatClient, Room } from '@ably/chat';
const chatClient: ChatClient; // existing ChatClient instance
// Get a room with default options
const room = await chatClient.rooms.get('general-chat');
// Always release when done
await chatClient.rooms.release('general-chat');
// Handle errors when options conflict
try {
// This will throw if 'game-room' already exists with different options
const room1 = await chatClient.rooms.get('game-room', {
typing: { heartbeatThrottleMs: 1000 }
});
const room2 = await chatClient.rooms.get('game-room', {
typing: { heartbeatThrottleMs: 2000 } // Different options!
});
} catch (error) {
if (error.code === 40000) {
console.error('Room already exists with different options');
}
}
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. This method only releases the reference
and detaches from Ably; it doesn't unsubscribe existing event listeners.
Note:
get calls for the same room.The unique identifier of the room to release
Promise that resolves when the room is fully released
import * as Ably from 'ably';
import { ChatClient } from '@ably/chat';
const chatClient: ChatClient; // existing ChatClient instance
// Get a room with default options and attach to it
const room = await chatClient.rooms.get('temporary-chat');
await room.attach();
// Do chat operations...
// When done, release the room
await chatClient.rooms.release('temporary-chat');
// The room object is now unusable
try {
await room.messages.send({ text: 'This will fail' });
} catch (error) {
console.error('Room has been released');
}
// To use the room again, get a new instance
const newRoom = await chatClient.rooms.get('temporary-chat');
// Handle release of non-existent rooms (no-op)
await chatClient.rooms.release('non-existent-room'); // Safe, does nothing
Manages the lifecycle of chat rooms.