SDK setup

Use these instructions to install, authenticate and instantiate the Chat SDK.

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 token authentication.

Sign up to Ably to create an API key in the dashboard or use the Control API to create an API key programmatically.

API keys and tokens have a set of capabilities assigned to them that specify which operations, such as subscribe or publish can be performed on which resources. To use the Chat SDK, the API key requires the following capabilities depending on which features are being used:

Feature Capabilities
Send and receive messages Publish, Subscribe
Message history Subscribe, History
Online status Subscribe, Presence
Room occupancy Subscribe, Channel Metadata
Typing indicators Publish, Subscribe, Presence
Room reactions Publish, Subscribe

When setting the capabilities for Chat, you need to apply them to either a wildcard resource, or a wildcard resource prefixed with the chat namespace, for example:

  • [chat]* and *, or
  • [*]*

The Chat SDK is built on top of the Ably Pub/Sub SDK and uses that to establish a connection with Ably.

Both SDKs are available as NPM modules and via CDN.

Install the Pub/Sub SDK and the Chat SDK:

npm install @ably/chat
Copied!

Import the SDKs into your project:

Select...
import * as Ably from 'ably'; import ChatClient from '@ably/chat';
Copied!

Reference the Pub/Sub SDK and the Chat SDK within your HTML file:

<script src="https://cdn.ably.com/lib/ably.min-2.js"></script> <script src="https://cdn.ably.com/lib/ably-chat.umd.cjs-0.js"></script> <script> const realtime = new Ably.Realtime({ key: '<loading API key, please wait>', clientId: '<clientId>'}); const chatClient = new AblyChat.ChatClient(realtime); </script>
Demo Only
Copied!

Instantiate a realtime client using the Pub/Sub SDK and pass the generated client into the Chat constructor.

Pass the ChatClient into the ChatClientProvider. The ChatClient instance will be available to all child components in your React component tree.

Select...
const realtimeClient = new Ably.Realtime({ key: '<loading API key, please wait>', clientId: '<clientId>'}); const chatClient = new ChatClient(realtimeClient);
Demo Only
Copied!

A ClientOptions object may be passed to the Pub/Sub SDK instance to further customize the connection, however at a minimum you must set an API key and provide a clientId to ensure that the client is identified.

Additional options can also be passed to the Chat client to customize the following properties:

Property Description
logHandler The function to call for each line of log output. The default is console.log.
logLevel The verbosity of the log output. Options are; trace, debug, info, warn, error or silent. The default is error.

Set the logHandler and logLevel properties when instantiating a client to configure your log handler:

Select...
const ably = new Ably.Realtime({ key: '<loading API key, please wait>', clientId: '<clientId>'}); const chatClient = new ChatClient(ably, {logHandler: logWriteFunc, logLevel: 'debug' });
Demo Only
Copied!

The logHandler property is your own function that will be called for each line of log output generated by the Chat SDK.

The logLevel sets the verbosity of logs that will be output by the SDK. The following log levels are available to set:

Level Description
trace Something routine and expected has occurred. This level will provide logs for the vast majority of operations and function calls.
debug Development information, messages that are useful when trying to debug library behavior, but superfluous to normal operation.
info Informational messages. Operationally significant to the library but not out of the ordinary.
warn Anything that is not immediately an error, but could cause unexpected behavior in the future. For example, passing an invalid value to an option. Indicates that some action should be taken to prevent future errors.
error A given operation has failed and cannot be automatically recovered. The error may threaten the continuity of operation.
silent No logging will be performed.
Authentication
v2.0