iOS/Swift
Listening to Presence Changes
Confused about "Listening to Presence Changes"?
Let us know how we can improve our documentation:
LAST EDIT Feb 16 2021
Of course, you want to listen to the user presence changes. This allows you to show a user as offline when they leave and update their status in real time. These 3 endpoints allow you to watch user presence:
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
// If you pass presence: true to channel.watch it will watch the list of user presence changes.
// Note that you can listen to at most 10 users using this API call
const channel = client.channel('messaging', 'my-conversation-123', {
members: ['john', 'jack'],
color: 'green'
});
const state = await channel.watch({ presence: true });
// queryChannels allows you to listen to the members of the channels that are returned
// so this does the same thing as above and listens to online status changes for john and jack
const channels = await client.queryChannels(
{ color: 'green' },
{ last_message_at: -1 },
{ presence: true }
);
// queryUsers allows you to listen to user presence changes for john and jack
const users = await client.queryUsers({
id: {
$in: [
'john',
'jack'
]
}},
{ id: -1 },
{ presence: true }
);
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
// If you pass presence: true to channel.watch it will watch the list of user presence changes.
// Note that you can listen to at most 10 users using this API call
val watchRequest = ChannelWatchRequest()
watchRequest.data["members"] = listOf("john", "jack")
channelController.watch(watchRequest).enqueue {
val channel = it.data()
}
// queryChannels allows you to listen to the members of the channels that are returned
// so this does the same thing as above and listens to online status changes for john and jack
val wathRequestWithPresence = ChannelWatchRequest()
wathRequestWithPresence.presence = true
wathRequestWithPresence.data["members"] = listOf("john", "jack")
channelController.watch(wathRequestWithPresence).enqueue {
val channel = it.data()
}
// queryUsers allows you to listen to user presence changes for john and jack
val offset = 0
val limit = 2
val usersFilter = Filters.`in`("id", listOf("john", "jack"))
val usersQuery = QueryUsersRequest(usersFilter, offset, limit)
usersQuery.presence = true
client.queryUsers(usersQuery).enqueue {
val users = it.data()
}
A users online status change can be handled via event delegation by subscribing to the user.presence.changed
event the same you do for any other event.