Connection

Open in

The Connection interface represents the connection to Ably and provides methods to monitor connection status changes. Access the connection via chatClient.connection.

Kotlin

1

val connection = chatClient.connection

Properties

The Connection interface has the following properties:

statusConnectionStatus
The current connection status.
The error that caused the connection to enter its current status, if any.

Subscribe to connection status changes

connection.onStatusChange(listener: ConnectionStatusListener): Subscription

Registers a listener to be notified of connection status changes.

Kotlin

1

2

3

4

5

6

7

8

9

10

11

12

val subscription = chatClient.connection.onStatusChange { change ->
    println("Status changed from ${change.previous} to ${change.current}")
    change.error?.let { error ->
        println("Error: $error")
    }
    change.retryIn?.let { retryIn ->
        println("Retrying in $retryIn ms")
    }
}

// To unsubscribe
subscription.unsubscribe()

Parameters

The onStatusChange() method takes the following parameters:

listenerrequiredConnectionStatusListener
A callback invoked on status changes.

Returns

Subscription

Returns a Subscription object.

Deregister the listener

subscription.unsubscribe(): Unit

Call unsubscribe() to deregister the connection status listener.

Collect connection status changes as a Flow

Connection.statusAsFlow(): Flow<ConnectionStatusChange>

Returns a Kotlin Flow that emits connection status changes. This is an extension function on the Connection interface that provides a reactive alternative to listener-based subscriptions.

Kotlin

1

2

3

4

5

6

7

import kotlinx.coroutines.launch

launch {
    chatClient.connection.statusAsFlow().collect { change ->
        println("Status changed from ${change.previous} to ${change.current}")
    }
}

Returns

Flow<ConnectionStatusChange>

Returns a Flow that emits ConnectionStatusChange events. The flow automatically manages the underlying subscription.

Example

Kotlin

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

import com.ably.chat.ConnectionStatus
import kotlinx.coroutines.launch

// Monitor connection status with a listener
val subscription = chatClient.connection.onStatusChange { change ->
    when (change.current) {
        ConnectionStatus.Connected ->
            println("Connected to Ably")
        ConnectionStatus.Disconnected ->
            println("Disconnected, will retry...")
        ConnectionStatus.Suspended ->
            println("Connection suspended, retrying in ${change.retryIn} ms")
        ConnectionStatus.Failed ->
            println("Connection failed: ${change.error}")
        else -> {}
    }
}

// Or use Flow for reactive collection
launch {
    chatClient.connection.statusAsFlow().collect { change ->
        println("Status: ${change.current}")
    }
}

// Check current status
println("Current status: ${chatClient.connection.status}")

// Clean up
subscription.unsubscribe()