import { ChannelOptionsMerger } from './channel-manager.js'; import { ChatApi } from './chat-api.js'; import { OccupancyEvent } from './events.js'; import { Logger } from './logger.js'; import { OccupancyData } from './occupancy-parser.js'; import { InternalRoomOptions } from './room-options.js'; import { Subscription } from './subscription.js'; import * as Ably from '@ably/pubsub-core'; /** * This interface is used to interact with occupancy in a chat room: subscribing to occupancy updates and * fetching the current room occupancy metrics. * * Get an instance via {@link Room.occupancy}. */ export interface Occupancy { /** * Subscribes to occupancy updates for the chat room. * * Receives updates whenever the number of connections or present members in the room changes. * This is useful for displaying active user counts, monitoring room capacity, or tracking * engagement metrics. * * **Note**: * - Requires {@link OccupancyOptions.enableEvents} to be true in the room's occupancy options. * - The room should be attached to receive occupancy events. * @param listener - Callback invoked when room occupancy changes * @returns Subscription object with an unsubscribe method * @throws An {@link Ably.ErrorInfo} with {@link ErrorCode.FeatureNotEnabledInRoom} if occupancy events are not enabled * @example * ```typescript * import * as Ably from '@ably/pubsub-device'; * import { ChatClient, OccupancyEvent } from '@ably/chat'; * * const chatClient: ChatClient; // existing ChatClient instance * * // Create room with occupancy events enabled * const room = await chatClient.rooms.get('conference-room', { * occupancy: { enableEvents: true } * }); * * * // Subscribe to occupancy updates * const subscription = room.occupancy.subscribe((event: OccupancyEvent) => { * const { connections, presenceMembers } = event.occupancy; * * console.log(`Room occupancy updated:`); * console.log(`Total connections: ${connections}`); * console.log(`Presence members: ${presenceMembers}`); * }); * * // Attach to the room to start receiving events * await room.attach(); * * // Later, unsubscribe when done * subscription.unsubscribe(); * ``` */ subscribe(listener: OccupancyListener): Subscription; /** * Fetches the current occupancy of the chat room from the server. * * Retrieves the latest occupancy metrics, including the number * of active connections and presence members. Use this for on-demand occupancy * checks or when occupancy events are not enabled. * * **Note**: This method uses the Ably Chat REST API and so does not require the room * to be attached to be called. * @returns Promise resolving to current occupancy data * @example * ```typescript * import * as Ably from '@ably/pubsub-device'; * import { ChatClient, OccupancyData } from '@ably/chat'; * * const chatClient: ChatClient; // existing ChatClient instance * * const room = await chatClient.rooms.get('webinar-room'); * * // Get current occupancy on demand * try { * const occupancy: OccupancyData = await room.occupancy.get(); * * console.log(`Current room statistics:`); * console.log(`Active connections: ${occupancy.connections}`); * console.log(`Presence members: ${occupancy.presenceMembers}`); * } catch (error) { * console.error('Failed to fetch occupancy:', error); * } * ``` */ get(): Promise; /** * Gets the latest occupancy data cached from realtime events. * * Returns the most recent occupancy metrics received via subscription. Returns undefined * if no occupancy events have been received yet since the room was attached. * * **Note**: * - Requires `enableEvents` to be true in the room's occupancy options. * - Returns undefined until the first occupancy event is received. * @returns Latest cached occupancy data or undefined if no events received * @throws An {@link Ably.ErrorInfo} with {@link ErrorCode.FeatureNotEnabledInRoom} if occupancy events are not enabled * @example * ```typescript * import * as Ably from '@ably/pubsub-device'; * import { ChatClient, OccupancyData } from '@ably/chat'; * * const chatClient: ChatClient; // existing ChatClient instance * * // Room with occupancy events enabled * const room = await chatClient.rooms.get('gaming-lobby', { * occupancy: { enableEvents: true } * }); * * // Subscribe to occupancy events * room.occupancy.subscribe((event) => { * console.log('Occupancy updated:', event.occupancy); * }); * * // Get cached occupancy instantly (after first event) * function displayCurrentOccupancy() { * const occupancy = room.occupancy.current; * * if (occupancy) { * console.log(`Current cached occupancy:`); * console.log(`Connections: ${occupancy.connections}`); * console.log(`Presence: ${occupancy.presenceMembers}`); * } else { * console.log('No occupancy data received yet, try fetching from server'); * } * } * * // Attach to the room to start receiving events * await room.attach(); * * ``` */ get current(): OccupancyData | undefined; } /** * A listener that is called when the occupancy of a chat room changes. * @param event The occupancy event. */ export type OccupancyListener = (event: OccupancyEvent) => void; /** * @inheritDoc */ export declare class DefaultOccupancy implements Occupancy { private readonly _roomName; private readonly _channel; private readonly _chatApi; private readonly _logger; private readonly _emitter; private readonly _roomOptions; private _latestOccupancyData?; private readonly _unsubscribeOccupancyEvents; /** * Constructs a new `DefaultOccupancy` instance. * @param roomName The unique identifier of the room. * @param channel An instance of the Realtime channel. * @param chatApi An instance of the ChatApi. * @param logger An instance of the Logger. * @param roomOptions The room options. */ constructor(roomName: string, channel: Ably.RealtimeChannel, chatApi: ChatApi, logger: Logger, roomOptions: InternalRoomOptions); /** * @inheritdoc */ subscribe(listener: OccupancyListener): Subscription; /** * @inheritdoc */ get(): Promise; /** * @inheritdoc */ get current(): OccupancyData | undefined; /** * An internal listener that listens for occupancy events from the underlying channel and translates them into * occupancy events for the public API. * @param message The inbound message containing occupancy data. */ private _internalOccupancyListener; /** * Merges the channel options for the room with the ones required for occupancy. * @param roomOptions The internal room options. * @returns A function that merges the channel options for the room with the ones required for occupancy. */ static channelOptionMerger(roomOptions: InternalRoomOptions): ChannelOptionsMerger; }