# Connection
The `Connection` interface represents the connection to Ably and provides methods to monitor connection status changes. Access the connection via `chatClient.connection`.
#### Kotlin
```
val connection = chatClient.connection
```
## Properties
The `Connection` interface has the following properties:
### Kotlin
```
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:
### Kotlin
```
import kotlinx.coroutines.launch
launch {
chatClient.connection.statusAsFlow().collect { change ->
println("Status changed from ${change.previous} to ${change.current}")
}
}
```
### Returns
`Flow
### Kotlin
```
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()
```
## Related Topics
- [ChatClient](https://ably.com/docs/chat/api/kotlin/chat-client.md): API reference for the ChatClient interface in the Ably Chat Kotlin SDK.
- [Rooms](https://ably.com/docs/chat/api/kotlin/rooms.md): API reference for the Rooms interface in the Ably Chat Kotlin SDK.
- [Room](https://ably.com/docs/chat/api/kotlin/room.md): API reference for the Room interface in the Ably Chat Kotlin SDK.
- [Messages](https://ably.com/docs/chat/api/kotlin/messages.md): API reference for the Messages interface in the Ably Chat Kotlin SDK.
- [Message](https://ably.com/docs/chat/api/kotlin/message.md): API reference for the Message interface in the Ably Chat Kotlin SDK.
- [MessageReactions](https://ably.com/docs/chat/api/kotlin/message-reactions.md): API reference for the MessageReactions interface in the Ably Chat Kotlin SDK.
- [Presence](https://ably.com/docs/chat/api/kotlin/presence.md): API reference for the Presence interface in the Ably Chat Kotlin SDK.
- [Occupancy](https://ably.com/docs/chat/api/kotlin/occupancy.md): API reference for the Occupancy interface in the Ably Chat Kotlin SDK.
- [Typing](https://ably.com/docs/chat/api/kotlin/typing.md): API reference for the Typing interface in the Ably Chat Kotlin SDK.
- [RoomReactions](https://ably.com/docs/chat/api/kotlin/room-reactions.md): API reference for the RoomReactions interface in the Ably Chat Kotlin SDK.
## Documentation Index
To discover additional Ably documentation:
1. Fetch [llms.txt](https://ably.com/llms.txt) for the canonical list of available pages.
2. Identify relevant URLs from that index.
3. Fetch target pages as needed.
Avoid using assumed or outdated documentation paths.