Occupancy

Occupancy enables you to view the number of users currently online in a room. This feature can be used to display user counts to highlight popular, or trending chat rooms.

Subscribe to a room’s occupancy by registering a listener. Occupancy events are emitted whenever the number of online users within a room changes. Use the occupancy.subscribe() method in a room to receive updates:

Select...
const {unsubscribe} = room.occupancy.subscribe((event) => { console.log(event); });
Copied!

The following is the structure of an occupancy event:

{ "connections": 103; "presenceMembers": 95; }
Copied!

The following are the properties of an occupancy event:

Property Description Type
connections The number of connections in the room. Number
presenceMembers The number of users entered into the presence set of the room. Number

Unsubscribe from room occupancy to remove previously registered listeners.

Use the unsubscribe() function returned in the subscribe() response to remove a listener:

Select...
// Initial subscription const { unsubscribe } = room.occupancy.subscribe((event) => { console.log(event); }); // To remove the listener unsubscribe();
Copied!

Use the occupancy.unsubscribeAll() method to remove all occupancy listeners in a room:

Select...
await room.occupancy.unsubscribeAll();
Copied!

The occupancy of a room can be retrieved in one-off calls instead of subscribing to updates.

Use the occupancy.get() method to retrieve the occupancy of a room:

Select...
const occupancy = await room.occupancy.get();
Copied!

The following is the structure of an occupancy event:

{ "connections": 103; "presenceMembers": 95; }
Copied!

The following are the properties of an occupancy event:

Property Description Type
connections The number of connections in the room. Number
presenceMembers The number of users entered into the presence set of the room. Number

Subscribe to room occupancy
v2.0