Connection management
The Kotlin Chat SDK provides connection management methods to handle subscription errors and restore connections when network issues occur. These methods give you full control over your chat app's connectivity and subscription state.
Connection status listener
The addConnectionStatusListener()
method provides real-time updates about your connection status.
Use this method to track subscription status and react to any subscription errors, such as network connectivity issues or authentication problems.
Method signature
This method takes the following parameters:
chat.addConnectionStatusListener(
callback: (ConnectionStatus) -> Unit
): AutoCloseable
Input
Parameter | Description |
---|---|
callback *Type: (ConnectionStatus) -> Unit Default: n/a | Function invoked on every connection status change with a ConnectionStatus object. |
Output
Type | Description |
---|---|
AutoCloseable | A handle you can call close() on to remove the listener. |
Remove the listener
Call close()
on the returned AutoCloseable
to remove the listener.
Connection status types
The connection status listener reports the following status types:
Status | Description |
---|---|
PN_CONNECTION_ONLINE | The connection has been established and is ready to receive real-time updates. |
PN_CONNECTION_OFFLINE | The connection has been intentionally terminated. |
PN_CONNECTION_ERROR | The connection was unexpectedly lost or failed to establish. Contains error details. |
ConnectionStatus
Property | Description |
---|---|
category *Type: ConnectionStatusCategory | Current connection status category. See Connection status types. |
exception Type: PubNubException? | Error details when category is PN_CONNECTION_ERROR ; null otherwise. |
Sample code
Set up a connection status listener to monitor your chat connection and automatically reconnect on errors.
val statusListener = chat.addConnectionStatusListener { status: ConnectionStatus ->
when (status.category) {
ConnectionStatusCategory.PN_CONNECTION_ONLINE -> {
println("Received status PN_CONNECTION_ONLINE")
}
ConnectionStatusCategory.PN_CONNECTION_OFFLINE -> {
println("Received status PN_CONNECTION_OFFLINE")
}
ConnectionStatusCategory.PN_CONNECTION_ERROR -> {
println("Received status PN_CONNECTION_ERROR with error: ${status.exception?.message}")
// Attempt to recover all subscriptions
chat.reconnectSubscriptions().async { result ->
result.onSuccess {
println("Reconnected subscriptions")
}.onFailure { ex ->
show all 25 linesReconnect subscriptions
The reconnectSubscriptions()
method restores the previous subscriptions with all subscribed channels and all added listeners. Call this method if disconnectSubscriptions()
was called previously or if you received PN_CONNECTION_ERROR
from the connection status listener to restore all existing subscriptions and listeners.
Method signature
chat.reconnectSubscriptions(): PNFuture<Unit>
Output
Type | Description |
---|---|
PNFuture<Unit> | A future that completes on success or fails with a PubNubException . |
Sample code
Reconnect all subscriptions after a connection error.
chat.reconnectSubscriptions().async { result ->
result.onSuccess {
// handle success
}.onFailure {
// handle failure
}
}
Disconnect subscriptions
The disconnectSubscriptions()
method disconnects (pauses) all active subscriptions and listeners. Call this method when you want to pause real-time updates.
Method signature
chat.disconnectSubscriptions(): PNFuture<Unit>
Output
Type | Description |
---|---|
PNFuture<Unit> | A future that completes on success or fails with a PubNubException . |
Sample code
Disconnect all subscriptions when you want to pause real-time updates.
chat.disconnectSubscriptions().async { result ->
result.onSuccess {
// handle success
}.onFailure {
// handle failure
}
}
Troubleshooting
The recommended workflow for handling subscription errors is:
- Initialize connection monitoring: Add the connection status listener immediately after creating the
Chat
object instance. - Handle connection errors: When receiving
PN_CONNECTION_ERROR
status, usereconnectSubscriptions()
to restore the connection. - Manual control: Use
disconnectSubscriptions()
when you want to manually pause real-time updates (for example, when the app goes to background). - Restore connection: Use
reconnectSubscriptions()
to restore subscriptions when needed.
This pattern ensures your chat app maintains stable connectivity and can automatically recover from network issues without requiring users to restart the application or lose their chat state.