The Occupancy interface provides methods for tracking the number of users in a chat room. Access it via room.occupancy.
1
val occupancy = room.occupancyProperties
The Occupancy interface has the following properties:
currentOccupancyData?enableEvents to be true in the room's occupancy options.Get current occupancy
suspend occupancy.get(): OccupancyDataFetches the current occupancy of the chat room from the server. Retrieves the latest occupancy metrics, including the number of active connections and presence members.
This method uses the Ably Chat REST API and does not require the room to be attached.
1
2
3
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 on failure.
Subscribe to occupancy events
occupancy.subscribe(listener: OccupancyListener): SubscriptionSubscribes 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. The room should be attached to receive occupancy events.
1
2
3
4
5
6
7
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:
listenerrequiredOccupancyEventReturns
Returns a Subscription object.
Unsubscribe from occupancy events
subscription.unsubscribe(): UnitCall unsubscribe() to stop receiving occupancy events.
Collect occupancy events as a Flow
Occupancy.asFlow(): Flow<OccupancyEvent>Returns a Kotlin Flow that emits occupancy events. This is an extension function on the Occupancy interface that provides a reactive alternative to listener-based subscriptions.
1
2
3
4
5
6
7
import kotlinx.coroutines.launch
launch {
room.occupancy.asFlow().collect { event ->
println("Connections: ${event.occupancy.connections}")
}
}Returns
Flow<OccupancyEvent>
Returns a Flow that emits OccupancyEvent events. The flow automatically manages the underlying subscription.
Example
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
31
32
33
34
35
36
37
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()