SDK setup
Use these instructions to install, authenticate and instantiate the Chat SDK.
Authentication
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 |
Update message | message-update-any or message-update-own |
Delete message | message-delete-any or message-delete-own |
Message history | subscribe , history |
Message reactions | annotation-publish , optionally annotation-subscribe |
Online status | subscribe , presence |
Room occupancy | subscribe , channel-metadata |
Typing indicators | publish , subscribe |
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]*
or[*]*
Install
The Chat SDK is built on top of the Ably Pub/Sub SDK and uses that to establish a connection with Ably.
NPM
Install the Pub/Sub SDK and the Chat SDK:
npm install @ably/chat
Currently, React is also required as a peer dependency. This will be removed in a future release.
npm install react
Import the SDKs into your project:
CDN
Reference the Pub/Sub SDK and the Chat SDK within your HTML file:
Instantiate a client
Instantiate a realtime client using the Pub/Sub SDK and pass the generated client into the Chat constructor.
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.
In many cases, a users unique application-specific identifier may be used as the clientId
to provide consistent identification for clients across your application.
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 . |
Logging
Set the logHandler
and logLevel
properties when instantiating a client to configure your log handler:
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. |