@ably/chat - v1.1.1
    Preparing search index...

    Interface Rooms

    Manages the lifecycle of chat rooms.

    interface Rooms {
        dispose(): Promise<void>;
        get(name: string, options?: RoomOptions): Promise<Room>;
        release(name: string): Promise<void>;
    }
    Index

    Methods

    • Disposes all rooms that are currently in the rooms map. This method releases all rooms concurrently and clears the rooms map.

      Returns Promise<void>

      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:

      • If options differ from an existing room, an error is thrown.
      • If get is called during a release, it waits for release to complete.
      • If release is called before get resolves, the promise rejects.

      Parameters

      • name: string

        The unique identifier of the room

      • Optionaloptions: RoomOptions

        Optional configuration for the room features

      Returns Promise<Room>

      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:

      • Calling release aborts any in-progress get calls for the same room.
      • The room object becomes unusable after release.

      Parameters

      • name: string

        The unique identifier of the room to release

      Returns Promise<void>

      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