Skip to main content

Monitor connection status

The Chat SDK provides a ConnectionStatusHandler that reports real-time changes in the connection state. Use it to update your UI or trigger business logic based on the current connection status.

Connection states

When the connection state changes, the SDK invokes your ConnectionStatusHandler callback with the current state. The table below describes each state.

StateValueDescription
NETWORK_UNAVAILABLE-1The device has no network connectivity.
CONNECTED0Successfully connected to the server.
CONNECTING1A connection attempt is in progress.
UNCONNECTED2Not connected. The app has not called connect yet.
KICKED_OFFLINE_BY_OTHER_CLIENT3The user signed in on another device, and this device was kicked offline.
TOKEN_INCORRECT4The access token is expired or invalid.
CONN_USER_BLOCKED6The user is blocked.
SIGNED_OUT12The user disconnected manually. See Disconnect.
SUSPENDED13The connection is temporarily suspended, usually due to a network issue. The SDK reconnects automatically.
TIMEOUT14The connection timed out. The SDK stops reconnecting. Handle the timeout in your app and call connect again if needed.
PROXY_UNAVAILABLE17The proxy server is unreachable.
USER_ABANDON19The user abandoned the connection.

Add or remove a connection status handler

The SDK supports multiple concurrent handlers. Register handlers early in the app lifecycle. Remove them when they are no longer needed to avoid memory leaks.

kotlin
// Add a connection status handler
NCEngine.addConnectionStatusHandler("CONNECTION_HANDLER_ID", object : ConnectionStatusHandler {
override fun onConnectionStatusChanged(event: ConnectionStatusChangedEvent) {
when (event.status) {
ConnectionStatus.CONNECTED -> {
Log.d("Connection", "Connected")
updateUI(connected = true)
}
ConnectionStatus.CONNECTING -> {
Log.d("Connection", "Connecting...")
showConnecting()
}
ConnectionStatus.SUSPENDED -> {
// Temporarily suspended — SDK reconnects automatically
Log.d("Connection", "Suspended")
showNetworkError()
}
ConnectionStatus.NETWORK_UNAVAILABLE -> {
Log.d("Connection", "Network unavailable")
showNetworkUnavailable()
}
ConnectionStatus.KICKED_OFFLINE_BY_OTHER_CLIENT -> {
Log.d("Connection", "Kicked offline")
showKickedOfflineDialog()
}
ConnectionStatus.TOKEN_INCORRECT -> {
Log.d("Connection", "Access token expired")
refreshTokenAndConnect()
}
else -> {
Log.d("Connection", "Status: ${event.status}")
}
}
}
})

// Remove the connection status handler
NCEngine.removeConnectionStatusHandler("CONNECTION_HANDLER_ID")

Parameters

ParameterTypeDescription
identifierStringA unique ID for this handler. Use it later to remove the handler.
handlerConnectionStatusHandlerThe callback invoked when the connection state changes.

Get the current connection status

Call getConnectionStatus() to retrieve the current connection state at any time.

tip

If the SDK begins reconnecting at the moment you call this method, the returned state may still show CONNECTED briefly.

kotlin
val currentState = NCEngine.getConnectionStatus()
when (currentState) {
ConnectionStatus.CONNECTED -> {
// Connected
}
ConnectionStatus.CONNECTING -> {
// Connecting
}
else -> {
// Other state
}
}