import { OccupancyListener } from '../../core/occupancy.js'; import { ChatStatusResponse } from '../types/chat-status-response.js'; import { Listenable } from '../types/listenable.js'; import { StatusParams } from '../types/status-params.js'; /** * The options for the {@link useOccupancy} hook. */ export interface UseOccupancyParams extends StatusParams, Listenable { /** * A listener that will be called whenever an occupancy event is received. * The listener is removed when the component unmounts. * @example * ```tsx * useOccupancy({ * listener: (occupancyEvent) => { * console.log('Occupancy changed:', occupancyEvent.occupancy); * } * }); * ``` */ listener?: OccupancyListener; } /** * The response type from the {@link useOccupancy} hook. */ export interface UseOccupancyResponse extends ChatStatusResponse { /** * The current number of users connected to the room, kept up to date by the hook. */ readonly connections: number; /** * The current number of users present in the room, kept up to date by the hook. */ readonly presenceMembers: number; } /** * React hook that provides real-time room occupancy information. * * This hook automatically tracks the number of connections and presence members in a room, * updating the counts in real-time as users join and leave. It integrates with the nearest * {@link ChatRoomProvider} and handles cleanup when the component unmounts. * * The hook provides both the current occupancy metrics as state values and allows you to * register listeners for occupancy change events. * * **Note**: This hook must be used within a {@link ChatRoomProvider} component tree. * **Note**: Room must be attached to receive real-time occupancy updates, typically the {@link ChatRoomProvider} handles this automatically. * @param params - Optional parameters for event listeners and room status callbacks * @returns A {@link UseOccupancyResponse} containing current occupancy metrics and room status * @throws An {@link Ably.ErrorInfo} with {@link chat-js!ErrorCode.ReactHookMustBeUsedWithinProvider | ReactHookMustBeUsedWithinProvider} When used outside of a {@link ChatRoomProvider} * @example Basic usage * ```tsx * import React from 'react'; * import { ChatClient, OccupancyEvent } from '@ably/chat'; * import { * ChatClientProvider, * ChatRoomProvider, * useOccupancy * } from '@ably/chat/react'; * * // Component that displays occupancy information * const RoomOccupancy = () => { * const { * connections, * presenceMembers, * connectionStatus, * roomStatus * } = useOccupancy({ * listener: (occupancyEvent: OccupancyEvent) => { * console.log('Occupancy changed:', occupancyEvent.occupancy); * }, * onDiscontinuity: (error) => { * console.error('Discontinuity detected:', error); * } * }); * * return ( *
*
👥 Total Connections: {connections}
*
🟢 Present Members: {presenceMembers}
*
* ); * }; * * const chatClient: ChatClient; // existing ChatClient instance * * // App component with providers * const App = () => { * return ( * * * * * * ); * }; * * export default App; * ``` */ export declare const useOccupancy: (params?: UseOccupancyParams) => UseOccupancyResponse;