Channels

Last Edit: Nov 27 2020

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.


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
//  }
//}
                    

final response = await channel.watch();

// readState is the list of read states for each user on the channel
List<Read> readState = response.read;
                    

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

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


channel.countUnread();
                    

channel.countUnread();
                    

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

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


channel.countUnreadMentions();
                    

channel.countUnreadMentions();
                    

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

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


// client-side
await client.markAllRead();

// mark all as read for one user server-side 
await serverSideClient.markAllRead({ user:  { id: 'myid' } });
                    

// mark all messages on all channels as read for one user, server-side 
$client->markAllRead('user-id');
                    

await client.markAllRead();
                    

Client.shared.markAllRead()
// or
Client.shared.markAllRead { result in
		// handle result
}