There are two main approaches depending on your use case :
-
Get unread counts on client level : [Getstream.io Unread documentation article]
- Retrieve unread count on user connection:
Total unread messages count AND unread channel count for a user are returned with the response when you setUser() with our API :
const user = await client.connectUser({ id: 'myid' }, token);
// response.me.total_unread_count returns the unread count
// response.me.unread_channels returns the count of channels with unread messages - Retrieve unread count using client level events :
To support updating the unread count in realtime, you can listen to these events.-
notification.added_to_channel
-
notification.removed_from_channel
-
notification.message_new
-
notification.mark_read
All 4 of these events include the fields:
total_unread_count
andunread_channels
. You can listen to them all at once like this:client.on((event) => {
if (event.total_unread_count !== undefined) {
console.log(event.total_unread_count);
}
if (event.unread_channels !== undefined) {
console.log(event.unread_channels);
}
}); -
- Retrieve unread count on user connection:
-
Get unread counts on channel level: [Getstream.io Unread channel documentation article]
- Retrieve unread messages count per channel, for the current user on the channel: Unread_messages_per_channel
- Retrieve the count of unread messages mentioning the current user on a channel: Unread_mentions_per_channel
- Retrieve unread count using notification events :
currentChannel.on('message.new', (e) => {
if (event.total_unread_count !== null) {
console.log(`unread messages count is now: ${event.total_unread_count}`);
}
if (event.unread_channels !== null) {
console.log(`unread channels count is now: ${event.unread_channels}`);
}
});
Please find below, our Blog tutorial on Stream Chat and Webhooks illustrating a message.new event usage :
How to Efficiently Use Webhooks and Stream Chat
Preconditions for a channel to be considered unread
Here are some of the conditions to be checked for before considering a channel as unread -
- The user needs to be a member of the channel
- The channel should not be muted
- The channel has unread messages OR If the channel has more messages after timestamp -
channel.lastRead
which gets set when you callchannel.markRead()
.- SDKs handle it by calling
channel.markRead()
whenever you open a channel
- SDKs handle it by calling
For the initial value, make use of the client.user.unread_channels
to access the unread channel count.
Comments
1 comment
Instead of only being able to access the total unread count and unread channel count on user connection, it would be very useful to get a mapping of channel ID to unread count.
Please sign in to leave a comment.