Get Twilio Chat User Channels sorted by last activity or unread count?
Asked Answered
O

1

7

We are developing a chat system where users can be in many chat rooms, and I'd like to be able to show the most recent channels first.

This could be either by the time the last message was sent, or even by the number of unread messages, as long as there is some order and I don't need to go through all the pages of channels and get additional metadata to sort it manually.

I can't see any options in the docs and even though the response metadata has a "key" set to "channels", I haven't been able to figure out a query parameter that can change that.

It seems like channels will always be returned ordered by the random unique channel ID, so for pretty much every use case you'd need to get all channels and sort manually. Is that the case or am I missing something?

Orebro answered 14/11, 2017 at 23:52 Comment(0)
B
6

Twilio developer evangelist here.

I'm afraid you cannot order the channels within the API right now. This feature is on the roadmap though, however I can't give any time estimates for it.

The solution for now is sorting manually. I will update once that changes though.

Beholden answered 16/11, 2017 at 0:50 Comment(13)
Is the solution available in latest SDK or yet to incorporate ?Henrik
Sorting channels in the API or SDK is not available yet. To sort channels you currently need to retrieve them and sort them yourself.Beholden
@Beholden Is there somewhere we can see there roadmap on chat features/fixes?Kitti
There is no public roadmap at the moment, no.Beholden
Really need this functionality to optimise perfomance for users with more channels.Kitti
@Beholden So, we need to fetch N channels and then fire additional N requests to get the last timestamped message? This is very costly operation. Is there any other solution?Kinsfolk
You can now retrieve the user's channel descriptors which return a paginated list of ChannelDescriptors which have a dateUpdated property for the last time the channel was updated as well as the lastConsumedMessageIndex for the current user. I imagine these properties could influence your sort and allow you to show new messages too.Beholden
But if you can't pass any sorting options to getUserChannelDescriptors() you'd still have to fetch every single page and then do the sorting yourself right ?Bout
Hmm, yes, there's also this option now, which is worth reading the docs on: media.twiliocdn.com/sdk/js/chat/releases/3.2.1/docs/…Beholden
@Beholden the dateUpdated property does not update with new messages, it only seems to update with channel metadata, like changing the friendlyName. Surely the expectation is that dateUpdated includes new messages to anyone looking at that cal, and the docs provide no information as to what causes this to update.Niccolo
@Niccolo descriptors don't update live, if you are looking to be notified when something updates, you should listen for the relevant event.Beholden
Hi @philnash, I'm not asking about updating live - I mean that the "dateUpdated" property does not change at any time based on messages. If a channel gets a new message, and you load everything from scratch, the dateUpdated for both channelDescriptor and channel will still be the last time channel metadata was changed, not when any messages have been received.Niccolo
Oh, I see, yes. I think the advised way to do this is actually to load all the user's subscribed channels, then use getLocalChannels({ criteria: 'lastMessage' ]) to find the order of the channels you want.Beholden

© 2022 - 2024 — McMap. All rights reserved.