Channels

LAST EDIT Apr 12 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)") 
    } 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// --- using Low Level Client 
// Get channel 
val queryChannelRequest = QueryChannelRequest() 
 
val channel = ChatClient.instance().queryChannel( 
    channelType = "channel-type", 
    channelId = "channel-id", 
    request = queryChannelRequest 
) 
    .execute() 
    .data() 
 
// readState is the list of read states for each user on the channel 
val readState: List<ChannelUserRead> = channel.read 
 
// --- Using ChatDomain --- 
val channel = ChatDomain.instance() 
    .watchChannel(cid = "messaging:123", messageLimit = 0) 
    .execute() 
    .data() 
    .toChannel() 
 
val readState : List<ChannelUserRead> = channel.read
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// --- Using Low Level Client 
QueryChannelRequest queryChannelRequest = QueryChannelRequest() 
 
Channel channel = ChatClient.instance().queryChannel( 
        "channel-type", 
        "channel-id", 
        queryChannelRequest 
) 
        .execute() 
        .data(); 
 
// readState is the list of read states for each user on the channel 
List<ChannelUserRead> readState = channel.getRead(); 
 
// --- Using ChatDomain --- 
Channel channel = ChatDomain.instance() 
        .watchChannel() 
        .invoke("messaging:123", 0) 
        .execute() 
        .data() 
        .toChannel(); 
 
List<ChannelUserRead> userReadList = channel.getRead();

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.unreadCount();
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()
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
// --- Using low level client 
// Get channel 
val queryChannelRequest = QueryChannelRequest() 
 
val channel = ChatClient.instance().queryChannel( 
    channelType = "channel-type", 
    channelId = "channel-id", 
    request = queryChannelRequest 
) 
    .execute() 
    .data() 
 
// Unread count for current user 
val unreadCount: Int? = channel.unreadCount 
 
// --- Using ChatDomain 
// Get channel controller 
val channelController = ChatDomain.instance() 
    .useCases 
    .watchChannel(cid = "messaging:123", messageLimit = 0) 
    .execute() 
    .data() 
 
//Unread count for current user 
val unreadCount: LiveData<Int?> = channelController.unreadCount
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
// --- Using low level client ---  
// Get channel 
QueryChannelRequest queryChannelRequest = new QueryChannelRequest(); 
 
Channel channel = ChatClient.instance().queryChannel( 
        "channel-type", 
        "channel-id", 
        queryChannelRequest 
) 
        .execute() 
        .data(); 
 
// Unread count for current user 
int unreadCount = channel.getUnreadCount(); 
 
// --- Using ChatDomain 
// Get channel controller 
ChannelController channelController = ChatDomain.instance() 
        .getUseCases() 
        .getWatchChannel() 
        .invoke("messaging:123", 0) 
        .execute() 
        .data(); 
 
LiveData<Integer> unreadCount = channelController.getUnreadCount();

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
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.markRead();
1
2
3
4
5
Client.shared.markAllRead() 
// or 
Client.shared.markAllRead { result in 
		// handle result 
}
1
2
3
4
5
6
7
ChatClient.instance().markAllRead().enqueue { result -> 
    if (result.isSuccess) { 
        //Handle success 
    } else { 
        //Handle failure 
    } 
}
1
2
3
4
5
6
7
ChatClient.instance().markAllRead().enqueue(result -> { 
    if (result.isSuccess) { 
        //Handle success 
    } else { 
        //Handle failure 
    } 
});