Stream Announces $38 Million Series B Funding Learn More →

Channels

LAST EDIT Feb 16 2021

When you retrieve a channel from the API (e.g. using query channels), the read state for all members is included in the response. This allows you to display which messages are read by each user. For each member, we include the last time he or she marked the channel as read.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const channel = client.channel('messaging', 'test'); 
await channel.watch(); 
 
console.log(channel.state.read); 
 
//{ '2fe6019c-872f-482a-989e-ecf4f786501b': 
//  { user:  
//    {  
//      id: '2fe6019c-872f-482a-989e-ecf4f786501b', 
//      role: 'user', 
//      created_at: '2019-04-24T13:09:19.664378Z', 
//      updated_at: '2019-04-24T13:09:23.784642Z', 
//      last_active: '2019-04-24T13:09:23.781641Z', 
//      online: true 
//    }, 
//    last_read: 2019-04-24T13:09:21.623Z 
//  } 
//}
1
2
3
4
final response = await channel.watch(); 
 
// readState is the list of read states for each user on the channel 
List<Read> readState = response.read;
1
2
3
4
5
6
7
8
9
10
let channel = Client.shared.channel(type: .messaging, id: "general") 
 
channel.watch { (result) in 
    do { 
        let response = try result.get() 
        print(response.messageReads) 
    } catch { 
        print("Error \(error)") 
    } 
}

Unread Messages Per Channel

Copied!

You can retrieve the count of unread messages for the current user on a channel like this:

1
channel.countUnread();
1
channel.countUnread();
1
2
3
4
5
6
let channel = Client.shared.channel(type: .messaging, id: "general") 
let subscription = channel.subscribeToUnreadCount { count in 
    // handle new unread count 
} 
// Cancel subscription when you want to stop receiving events 
subscription.cancel()

Unread Mentions Per Channel

Copied!

You can retrieve the count of unread messages mentioning the current user on a channel like this:

1
channel.countUnreadMentions();
1
channel.countUnreadMentions();
1
2
3
4
5
6
7
let channel = Client.shared.channel(type: .messaging, id: "general") 
let subscription = channel.subscribeToUnreadCount { count in 
    // handle new unread count 
		print(count.mentionedMessages) 
} 
// Cancel subscription when you want to stop receiving events 
subscription.cancel()

Mark All As Read

Copied!

You can mark all channels as read for a user like this:

1
2
3
4
5
// client-side 
await client.markAllRead(); 
 
// mark all as read for one user server-side  
await serverSideClient.markAllRead({ user:  { id: 'myid' } });
1
2
// mark all messages on all channels as read for one user, server-side  
$client->markAllRead('user-id');
1
await client.markAllRead();
1
2
3
4
5
Client.shared.markAllRead() 
// or 
Client.shared.markAllRead { result in 
		// handle result 
}