import { ChatClientOptions } from './config.js'; import { Connection } from './connection.js'; import { Rooms } from './rooms.js'; import * as Ably from '@ably/pubsub-core'; /** * This is the core client for Ably chat. It provides access to chat rooms. * * Create an instance using {@link createChatClient}. */ export interface ChatClient { /** * Provides access to the rooms instance for creating and managing chat rooms. * @returns The Rooms instance for managing chat rooms * @example * ```typescript * const chatClient: ChatClient; // existing ChatClient instance * * // Get a room with default options * const room = await chatClient.rooms.get('general-chat'); * * // Get a room with custom options (merges with defaults) * const configuredRoom = await chatClient.rooms.get('team-chat', { * typing: { heartbeatThrottleMs: 1000 } * }); * * // Release a room when done * await chatClient.rooms.release('general-chat'); * ``` */ get rooms(): Rooms; /** * Provides access to the underlying connection to Ably for monitoring connectivity. * @returns The Connection instance * @example * ```typescript * const chatClient: ChatClient; // existing ChatClient instance * * // Check current connection status * console.log('Status:', chatClient.connection.status); * console.log('Error:', chatClient.connection.error); * * // Monitor connection changes * const { off } = chatClient.connection.onStatusChange((change) => { * console.log(`Connection: ${change.previous} -> ${change.current}`); * }); * ``` */ get connection(): Connection; /** * Returns the clientId of the current client, if known. * * **Important** When using an Ably key for authentication, this value is determined immediately. If using a token, * the clientId is not known until the client has successfully connected to and authenticated with * the server. Use the `chatClient.connection.status` to check the connection status. * @returns The clientId, or undefined if unknown. */ get clientId(): string | undefined; /** * Provides direct access to the underlying Ably Realtime client. * * Use this for advanced scenarios requiring direct Ably access. Most chat * operations should use the high-level chat SDK methods instead. * * **Note**: Directly interacting with the Ably Realtime client can lead to * unexpected behavior. * @returns The underlying Ably Realtime client instance */ get pubsubClient(): Ably.PubSubRealtimeClient; /** * The configuration options used to initialize the chat client. * @returns The resolved client options including defaults */ get clientOptions(): ChatClientOptions; /** * Disposes of the ChatClient instance and releases all resources. * * Releases all chat rooms, removes event listeners, and cleans up connections. * After calling dispose, the ChatClient instance is no longer usable. This should * be called when you're completely done with the chat functionality. * * **Note**: This will release ALL rooms managed by this ChatClient and the ChatClient cannot be reused after disposal. * @returns Promise that resolves when all resources are released * @example * ```typescript * import { ChatClient } from '@ably/chat'; * * const chatClient: ChatClient; // existing ChatClient instance * * // Use the chat client * const roomOne = await chatClient.rooms.get('general-chat'); * const roomTwo = await chatClient.rooms.get('random-chat'); * * // ... chat operations ... * * // Clean up when completely done * try { * await chatClient.dispose(); * console.log('Chat client disposed successfully'); * } catch (error) { * console.error('Failed to dispose chat client:', error); * } * * ``` */ dispose(): Promise; } /** * Creates a new {@link ChatClient} instance for interacting with Ably Chat. * * The ChatClient is the main entry point for the Ably Chat SDK. It requires a Realtime client * and provides access to chat rooms through the rooms property. * * **Important**: The Ably Realtime client must have a clientId set. This identifies * the user in chat rooms and is required for all chat operations. * * **NOTE**: You can provide optional overrides to the {@link ChatClient}, these will be merged * with the default options. See {@link ChatClientOptions} for the available options. * @param realtime - An initialized Ably Realtime client with a configured clientId * @param clientOptions - Optional configuration for the chat client * @returns A new {@link ChatClient} instance. * @example * ```typescript * import { createClient } from '@ably/pubsub-device'; * import { createChatClient, LogLevel } from '@ably/chat'; * * // Preferred in production: Use auth URL that returns a JWT * const realtimeClientWithJWT = createClient({ * authUrl: '/api/ably-auth', // Your server endpoint that returns a JWT with clientId * authMethod: 'POST' * }); * * const chatClient = createChatClient(realtimeClientWithJWT) *``` * @example *```typescript * // Alternative for development: Set clientId directly (requires API key) * const realtimeClientWithKey = createClient({ * key: 'your-ably-api-key', * clientId: 'user-123' * }); * * const chatClient = createChatClient(realtimeClientWithKey) * ``` * @example * ```typescript * const realtimeClient = createClient({ * authUrl: '/api/ably-auth', * authMethod: 'POST' * }); * * // With custom logging configuration: Defaults to LogLevel.Info and console logging * const chatClientWithLogging = createChatClient(realtimeClient, { * logLevel: LogLevel.Debug, * logHandler: (message, level, context) => { * // Send to your logging service * yourLoggerInstance.log({ * level, * message, * context, * timestamp: new Date() * }); * } * }); * ``` */ export declare const createChatClient: (realtime: Ably.PubSubRealtimeClient, clientOptions?: ChatClientOptions) => ChatClient;