On this page

Connection management

Handle subscription errors and restore connections when network issues occur.

Connection status listener

Monitor connection status in real time to track subscription state and react to network or authentication errors.

Method signature

This method takes the following parameters:

1chat.addConnectionStatusListener(
2 callback: (ConnectionStatus) -> Unit
3): AutoCloseable

Input

* required
ParameterDescription
callback *
Type: (ConnectionStatus) -> Unit
Default:
n/a
Function invoked on every connection status change with a ConnectionStatus object.

Output

TypeDescription
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:

StatusDescription
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

* required
PropertyDescription
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.

1val statusListener = chat.addConnectionStatusListener { status: ConnectionStatus ->
2 when (status.category) {
3 ConnectionStatusCategory.PN_CONNECTION_ONLINE -> {
4 println("Received status PN_CONNECTION_ONLINE")
5 }
6 ConnectionStatusCategory.PN_CONNECTION_OFFLINE -> {
7 println("Received status PN_CONNECTION_OFFLINE")
8 }
9 ConnectionStatusCategory.PN_CONNECTION_ERROR -> {
10 println("Received status PN_CONNECTION_ERROR with error: ${status.exception?.message}")
11 // Attempt to recover all subscriptions
12 chat.reconnectSubscriptions().async { result ->
13 result.onSuccess {
14 println("Reconnected subscriptions")
15 }.onFailure { ex ->
show all 25 lines

Reconnect subscriptions

Restore previous subscriptions with all subscribed channels and listeners. Call after disconnectSubscriptions() or when receiving PN_CONNECTION_ERROR.

Method signature

1chat.reconnectSubscriptions(): PNFuture<Unit>

Output

TypeDescription
PNFuture<Unit>
A future that completes on success or fails with a PubNubException.

Sample code

Reconnect all subscriptions after a connection error.

1chat.reconnectSubscriptions().async { result ->
2 result.onSuccess {
3 // handle success
4 }.onFailure {
5 // handle failure
6 }
7}

Disconnect subscriptions

Pause all active subscriptions and listeners.

Method signature

1chat.disconnectSubscriptions(): PNFuture<Unit>

Output

TypeDescription
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.

1chat.disconnectSubscriptions().async { result ->
2 result.onSuccess {
3 // handle success
4 }.onFailure {
5 // handle failure
6 }
7}

Troubleshooting

Recommended workflow for handling subscription errors:

  1. Initialize monitoring: Add the connection status listener immediately after creating the Chat instance.

  2. Handle errors: On PN_CONNECTION_ERROR, call reconnectSubscriptions().

  3. Manual control: Call disconnectSubscriptions() when pausing real-time updates (for example, when the app backgrounds).

  4. Restore connection: Call reconnectSubscriptions() to resume.

This pattern maintains stable connectivity and automatic recovery from network issues.

Last updated on