ChatClient

Open in

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

1

2

3

4

import { LogLevel } from '@ably/chat'

const realtimeClient = new Ably.Realtime({ key: 'demokey:*****', clientId: '<clientId>'});
const chatClient = new ChatClient(realtimeClient, { logLevel: LogLevel.Error });
API key:
DEMO ONLY

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.

Properties

The ChatClient interface has the following properties:

roomsRooms
The rooms object, used to get or create chat room instances.
connectionConnection
The connection object, used to monitor the connection status with Ably.
clientIdString or Undefined
The client ID configured on the underlying Ably Realtime client. Used to identify the current user. May be undefined until authenticated with a token.
realtimeAbly.Realtime
The underlying Ably Realtime client instance.
clientOptionsChatClientOptions
The resolved configuration options with defaults applied.

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

1

2

3

4

5

6

7

8

9

import * as Ably from 'ably';
import { ChatClient } from '@ably/chat';

const realtimeClient = new Ably.Realtime({
  key: 'demokey:*****',
  clientId: 'user-123'
});

const chatClient = new ChatClient(realtimeClient, clientOptions);
API key:
DEMO ONLY

Parameters

The ChatClient() constructor takes the following parameters:

realtimerequiredAbly.Realtime
An instance of the Ably Realtime client, configured with your API key and a clientId. The clientId is required for all chat operations.
clientOptionsoptionalChatClientOptions
Configuration options for the Chat client.

Dispose of the chat client

chatClient.dispose(): Promise<void>

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

1

await chatClient.dispose();

Returns

Promise<void>

Returns a promise. The promise is fulfilled when the client has been disposed, or rejected with an 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.

codeNumber
Ably-specific error code.
statusCodeNumber
HTTP status code corresponding to this error, where applicable.
messageString
Additional information about the error.
causeString, ErrorInfo, or Error
The underlying cause of the error, where applicable.

Example

JavaScript

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

import * as Ably from 'ably';
import { ChatClient, LogLevel } from '@ably/chat';

const realtimeClient = new Ably.Realtime({
  key: 'demokey:*****',
  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);
API key:
DEMO ONLY