Skip to main content

Connect

The client must successfully connect to the chat server before it can send and receive messages.

Prerequisites

  • You have obtained a user token from your application server.
  • You have completed SDK initialization (NCChatUI.initialize(...)).
  • We recommend setting up connection status monitoring first. See Monitor connection status.

Connect to the chat server

The Chat UI SDK provides the following connection method:

kotlin
NCChatUI.connect(params, handler)

Parameters

ParameterTypeDescription
paramsConnectParamsConnection parameters. token is required; timeout and reconnectKickEnable are optional.
handlerConnectHandlerConnection result callback. Can be null.

ConnectParams fields:

FieldTypeDescription
tokenStringUser identity token issued by your server (required).
timeoutintConnection timeout in seconds. Default is -1 (SDK default behavior).
reconnectKickEnablebooleanWhether to keep existing online devices during reconnection conflicts. Default is false.

Example

kotlin
val token = "user_token"

val params = ConnectParams(token).apply {
timeout = 10 // Optional: connection timeout in seconds
reconnectKickEnable = false // Optional
}

NCChatUI.connect(params, object : ConnectHandler {
override fun onDatabaseOpened(isRecreated: Boolean, error: NCError?) {
if (error == null) {
// Local database is ready; you can display cached channels and messages
}
}

override fun onResult(userId: String?, error: NCError?) {
if (error == null) {
// Connection successful
} else {
// Connection failed: error.code / error.message
}
}
})

Callbacks

ConnectHandler provides two callbacks:

  • onDatabaseOpened(boolean isRecreated, NCError error)
    • Called when the local database is opened.
    • error == null indicates the database is ready.
  • onResult(String userId, NCError error)
    • Called when the connection attempt completes.
    • error == null indicates success. Otherwise, check the error code and message.

Notes

  • You must call connect() after SDK initialization.
  • Typically, you only need to connect once per application lifecycle. The SDK handles reconnection automatically when the network is interrupted.
  • To disconnect manually, use NCChatUI.disconnect() or NCChatUI.logout(). See Disconnect.