The ChatClient interface 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 factory function.
1
2
3
4
5
6
7
8
9
10
11
12
import com.ably.chat.ChatClient
import com.ably.chat.LogLevel
import io.ably.lib.realtime.AblyRealtime
import io.ably.lib.types.ClientOptions
val realtimeClient = AblyRealtime(ClientOptions().apply {
key = "demokey:*****"
clientId = "<clientId>"
})
val chatClient = 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.
Properties
The ChatClient interface has the following properties:
roomsRoomsconnectionConnectionclientIdString?realtimeAblyRealtimeclientOptionsChatClientOptionsCreate a chat client
ChatClient(realtimeClient: AblyRealtime, clientOptions: ChatClientOptions = buildChatClientOptions()): ChatClientCreate a new ChatClient instance by passing an Ably Realtime client and optional configuration options. ChatClient is a top-level factory function, not a class constructor.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import com.ably.chat.ChatClient
import com.ably.chat.buildChatClientOptions
import com.ably.chat.LogLevel
import io.ably.lib.realtime.AblyRealtime
import io.ably.lib.types.ClientOptions
val realtimeClient = AblyRealtime(ClientOptions().apply {
key = "demokey:*****"
clientId = "user-123"
})
val chatClient = ChatClient(realtimeClient, buildChatClientOptions {
logLevel = LogLevel.Debug
})A DSL builder overload is also available:
ChatClient(realtimeClient: AblyRealtime, init: MutableChatClientOptions.() -> Unit): ChatClient1
2
3
val chatClient = ChatClient(realtimeClient) {
logLevel = LogLevel.Debug
}Parameters
The ChatClient() factory function takes the following parameters:
realtimeClientrequiredAblyRealtimeclientId. The clientId is required for all chat operations.clientOptionsoptionalChatClientOptionsChatException
A specialized exception class for errors that occur during chat operations. ChatException extends RuntimeException and wraps an ErrorInfo object containing detailed error information. All suspend functions in the SDK throw ChatException on failure.
errorInfoErrorInfomessageString?causeThrowable?1
2
3
4
5
6
7
8
9
import com.ably.chat.ChatException
try {
room.messages.send(text = "Hello!")
} catch (e: ChatException) {
println("Error code: ${e.errorInfo.code}")
println("Message: ${e.errorInfo.message}")
println("Status: ${e.errorInfo.statusCode}")
}ErrorInfo
A standardized, generic Ably error object that contains an Ably-specific status code, and a generic HTTP status code. All errors thrown by the SDK are compatible with the ErrorInfo structure.
codeIntstatusCodeIntmessageStringhrefString?causeErrorInfo?requestIdString?Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import com.ably.chat.ChatClient
import com.ably.chat.LogLevel
import io.ably.lib.realtime.AblyRealtime
import io.ably.lib.types.ClientOptions
val realtimeClient = AblyRealtime(ClientOptions().apply {
key = "demokey:*****"
clientId = "user-123"
})
val chatClient = ChatClient(realtimeClient) {
logLevel = LogLevel.Debug
}
// Access rooms
val room = chatClient.rooms.get("my-room")
// Check connection status
println(chatClient.connection.status)
// Get current user ID
println(chatClient.clientId)