import { StatusSubscription } from './subscription.js'; import * as Ably from '@ably/pubsub-core'; /** * The different states that the connection can be in through its lifecycle. */ export declare enum ConnectionStatus { /** * A temporary state for when the library is first initialized. */ Initialized = "initialized", /** * The library is currently connecting to Ably. */ Connecting = "connecting", /** * The library is currently connected to Ably. */ Connected = "connected", /** * The library is currently disconnected from Ably, but will attempt to reconnect. */ Disconnected = "disconnected", /** * The library is in an extended state of disconnection, but will attempt to reconnect. */ Suspended = "suspended", /** * The library is currently disconnected from Ably and will not attempt to reconnect. */ Failed = "failed", /** * An explicit request by the developer to close the connection has been sent to the Ably service. * If a reply is not received from Ably within a short period of time, the connection is forcibly * terminated and the connection status becomes Closed. */ Closing = "closing", /** * The connection has been explicitly closed by the client. In the closed state, no reconnection * attempts are made automatically. No connection state is preserved by the service or the library. */ Closed = "closed" } /** * Represents a change in the status of the connection. */ export interface ConnectionStatusChange { /** * The new status of the connection. */ current: ConnectionStatus; /** * The previous status of the connection. */ previous: ConnectionStatus; /** * An error that provides a reason why the connection has * entered the new status, if applicable. */ error?: Ably.ErrorInfo; /** * The time in milliseconds that the client will wait before attempting to reconnect. */ retryIn?: number; } /** * A function that can be called when the connection status changes. * @param change The change in status. */ export type ConnectionStatusListener = (change: ConnectionStatusChange) => void; /** * Represents a connection to Ably. */ export interface Connection { /** * The current status of the connection. * @returns The current ConnectionStatus value * @example * ```typescript * import { ChatClient, ConnectionStatus } from '@ably/chat'; * * const chatClient: ChatClient; // existing ChatClient instance * * // Check connection status * if (chatClient.connection.status === ConnectionStatus.Connected) { * console.log('Connected to Ably'); * } else if (chatClient.connection.status === ConnectionStatus.Failed) { * console.error('Connection failed'); * } * * // Use status for conditional logic * function canAttachToRoom(): boolean { * return chatClient.connection.status === ConnectionStatus.Connected; * } * ``` */ get status(): ConnectionStatus; /** * The error that caused the connection to enter its current status, if any. * @returns ErrorInfo if an error caused the current status, undefined otherwise * @example * ```typescript * import { ChatClient, ConnectionStatus } from '@ably/chat'; * * const chatClient: ChatClient; // existing ChatClient instance * * // Check for connection errors * if (chatClient.connection.error) { * console.error('Connection error:', chatClient.connection.error.message); * console.error('Error code:', chatClient.connection.error.code); * } * // Monitor for errors during status changes * chatClient.connection.onStatusChange((change) => { * if (change.error) { * reportErrorToMonitoring(change.error); * } * }); * ``` */ get error(): Ably.ErrorInfo | undefined; /** * Registers a listener to be notified of connection status changes. * * Status changes indicate the connection lifecycle, including connecting, * connected, disconnected, suspended, and failed states. Use this to monitor * connection health and handle network issues. * @param listener - Callback invoked when the connection status changes * @returns Subscription object with an off method to unregister * @example * ```typescript * import * as Ably from '@ably/pubsub-device'; * import { ChatClient, ConnectionStatus } from '@ably/chat'; * * const chatClient: ChatClient; // existing ChatClient instance * * // Monitor connection status changes * const { off } = chatClient.connection.onStatusChange((change) => { * console.log(`Connection: ${change.previous} -> ${change.current}`); * * // Handle different connection states.. * switch (change.current) { * case ConnectionStatus.Connected: * console.log('✅ Connected to Ably'); * enableChatFeatures(); * hideConnectionWarning(); * break; * * case ConnectionStatus.Failed: * console.error('❌ Connection failed permanently'); * if (change.error) { * console.error('Failure reason:', change.error.message); * showErrorMessage(`Connection failed: ${change.error.message}`); * } * requireManualReconnection(); * break; * * // Other states: Connecting, Disconnected, Suspended * } * }); * * // Clean up when done * off(); * ``` */ onStatusChange(listener: ConnectionStatusListener): StatusSubscription; } /** * An internal interface for the connection with additional methods. */ export interface InternalConnection extends Connection { }