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?

Bonnici
  • 1,596
  • 2
  • 12
  • 21

1 Answers1

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.

philnash
  • 70,667
  • 10
  • 60
  • 88
  • Is the solution available in latest SDK or yet to incorporate ? – Mohammed Shakeer Nov 19 '17 at 08:45
  • 1
    Sorting channels in the API or SDK is not available yet. To sort channels you currently need to retrieve them and sort them yourself. – philnash Nov 19 '17 at 22:01
  • @philnash Is there somewhere we can see there roadmap on chat features/fixes? – kabangi julius Dec 02 '17 at 04:39
  • There is no public roadmap at the moment, no. – philnash Dec 03 '17 at 17:56
  • 3
    Really need this functionality to optimise perfomance for users with more channels. – kabangi julius Dec 26 '17 at 12:00
  • @philnash 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? – svlada Jan 21 '19 at 09:10
  • You can now retrieve the [user's channel descriptors](http://media.twiliocdn.com/sdk/js/chat/releases/3.2.0/docs/Client.html#getUserChannelDescriptors__anchor) 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. – philnash Jan 21 '19 at 21:21
  • 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 ? – Michael Parton Apr 02 '19 at 00:38
  • Hmm, yes, there's also this option now, which is worth reading the docs on: http://media.twiliocdn.com/sdk/js/chat/releases/3.2.1/docs/Client.html#getLocalChannels__anchor – philnash Apr 02 '19 at 00:51
  • @philnash 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. – Brettins Jun 21 '20 at 19:37
  • @Brettins descriptors don't update live, if you are looking to be notified when something updates, you should listen for the relevant event. – philnash Jun 22 '20 at 06:28
  • 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. – Brettins Jun 23 '20 at 16:59
  • 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' ])`](http://media.twiliocdn.com/sdk/js/chat/releases/3.3.0/docs/Client.html#getLocalChannels) to find the order of the channels you want. – philnash Jun 25 '20 at 05:13