# ChatClient The `ChatClient` class is the main entry point for using the Ably Chat SDK. It provides access to chat rooms, connection management, and the underlying Ably Realtime client. The Chat SDK is built on top of the Ably Pub/Sub SDK and uses that to establish a connection with Ably. Instantiate a realtime client using the Pub/Sub SDK and pass the generated client into the Chat constructor. #### Javascript ``` import { LogLevel } from '@ably/chat' const realtimeClient = new Ably.Realtime({ key: 'your-api-key', clientId: ''}); const chatClient = new ChatClient(realtimeClient, { logLevel: LogLevel.Error }); ``` An API key is required to authenticate with Ably. API keys are used either to authenticate directly with Ably using basic authentication, or to generate tokens for untrusted clients using [JWT authentication](https://ably.com/docs/auth/token.md#jwt). ## Properties The `ChatClient` interface has the following properties: | Property | Description | Type | | --- | --- | --- | | rooms | The rooms object, used to get or create chat room instances. | [Rooms](https://ably.com/docs/chat/api/javascript/rooms.md) | | connection | The connection object, used to monitor the connection status with Ably. | [Connection](https://ably.com/docs/chat/api/javascript/connection.md) | | clientId | The client ID configured on the underlying Ably Realtime client. Used to identify the current user. May be `undefined` until authenticated with a token. | String or Undefined | | realtime | The underlying Ably Realtime client instance. | Ably.Realtime | | clientOptions | The resolved configuration options with defaults applied. |
|
| Property | Required | Description | Type | | --- | --- | --- | --- | | logLevel | Optional | The logging level to use. Default: `LogLevel.Error`. |
| | logHandler | Optional | A custom log handler function that receives log messages from the SDK. |
|
| Value | Description | | --- | --- | | Trace | Log all messages. | | Debug | Log debug, info, warn, and error messages. | | Info | Log info, warn, and error messages. | | Warn | Log warn and error messages. | | Error | Log error messages only. | | Silent | Disable logging. |
| Parameter | Required | Description | Type | | --- | --- | --- | --- | | message | Required | The log message. | String | | level | Required | The severity level of the log message. |
| | context | Optional | Additional contextual data associated with the log entry. |
|
| Property | Description | Type | | --- | --- | --- | | | Additional contextual key-value pairs associated with a log message. | `Record` |
## Create a new chat client `new ChatClient(realtime: Realtime, clientOptions?: ChatClientOptions)` Create a new `ChatClient` instance by passing an Ably Realtime client and optional configuration options. ### Javascript ``` import * as Ably from 'ably'; import { ChatClient } from '@ably/chat'; const realtimeClient = new Ably.Realtime({ key: 'your-api-key', clientId: 'user-123' }); const chatClient = new ChatClient(realtimeClient, clientOptions); ``` ### Parameters The `ChatClient()` constructor takes the following parameters: | Parameter | Required | Description | Type | | --- | --- | --- | --- | | realtime | Required | An instance of the Ably Realtime client, configured with your API key and a `clientId`. The `clientId` is required for all chat operations. | Ably.Realtime | | clientOptions | Optional | Configuration options for the Chat client. |
|
## Dispose of the chat client `chatClient.dispose(): Promise` Disposes of the ChatClient instance and releases all resources, including all chat rooms. After calling this method, the ChatClient instance is no longer usable. ### Javascript ``` await chatClient.dispose(); ``` ### Returns `Promise` Returns a promise. The promise is fulfilled when the client has been disposed, or rejected with an [`ErrorInfo`](#errorinfo) object. ## ErrorInfo A standardized, generic Ably error object that contains an Ably-specific status code, and a generic HTTP status code. All errors returned from Ably are compatible with the `ErrorInfo` structure. | Property | Description | Type | | --- | --- | --- | | code | Ably-specific error code. | Number | | statusCode | HTTP status code corresponding to this error, where applicable. | Number | | message | Additional information about the error. | String | | cause | The underlying cause of the error, where applicable. | String, ErrorInfo, or Error |
## Example ### Javascript ``` import * as Ably from 'ably'; import { ChatClient, LogLevel } from '@ably/chat'; const realtimeClient = new Ably.Realtime({ key: 'your-api-key', clientId: 'user-123' }); const chatClient = new ChatClient(realtimeClient, { logLevel: LogLevel.Debug }); // Access rooms const room = await chatClient.rooms.get('my-room'); // Check connection status console.log(chatClient.connection.status); // Get current user ID console.log(chatClient.clientId); ```