# Occupancy
The `Occupancy` interface provides methods for tracking the number of users in a chat room. Access it via `room.occupancy`.
#### Kotlin
```
val occupancy = room.occupancy
```
## Properties
The `Occupancy` interface has the following properties:
### Kotlin
```
val data = room.occupancy.get()
println("Connections: ${data.connections}")
println("Presence members: ${data.presenceMembers}")
```
### Returns
`OccupancyData`
This is a suspend function. It returns the current occupancy data, or throws a [`ChatException`](https://ably.com/docs/chat/api/kotlin/chat-client.md#chatexception) on failure.
## Subscribe to occupancy events
`occupancy.subscribe(listener: OccupancyListener): Subscription`
Subscribes to occupancy updates for the chat room. Receives updates whenever the number of connections or present members in the room changes.
Requires `enableEvents` to be `true` in the [room's occupancy options](https://ably.com/docs/chat/api/kotlin/rooms.md#get). The room should be [attached](https://ably.com/docs/chat/api/kotlin/room.md#attach) to receive occupancy events.
### Kotlin
```
val subscription = room.occupancy.subscribe { event ->
println("Connections: ${event.occupancy.connections}")
println("Presence members: ${event.occupancy.presenceMembers}")
}
// To stop receiving occupancy updates
subscription.unsubscribe()
```
### Parameters
The `subscribe()` method takes the following parameters:
### Kotlin
```
import kotlinx.coroutines.launch
launch {
room.occupancy.asFlow().collect { event ->
println("Connections: ${event.occupancy.connections}")
}
}
```
### Returns
`Flow
### Kotlin
```
import com.ably.chat.asFlow
import kotlinx.coroutines.launch
val room = chatClient.rooms.get("my-room") {
occupancy {
enableEvents = true // Required for subscribe() and current property
}
}
room.attach()
// Subscribe to occupancy changes with a listener
val subscription = room.occupancy.subscribe { event ->
val connections = event.occupancy.connections
val presenceMembers = event.occupancy.presenceMembers
println("Room has $connections connections and $presenceMembers presence members")
}
// Or use Flow for reactive collection
launch {
room.occupancy.asFlow().collect { event ->
println("Occupancy updated: ${event.occupancy.connections} connections")
}
}
// Fetch occupancy on-demand (works even without enableEvents)
val occupancy = room.occupancy.get()
println("Initial occupancy: ${occupancy.connections} connections")
// Access cached occupancy (requires enableEvents)
val cached = room.occupancy.current
cached?.let {
println("Cached occupancy: ${it.connections}")
}
// 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.
- [Connection](https://ably.com/docs/chat/api/kotlin/connection.md): API reference for the Connection 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.
- [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.