7

I'm trying to get the number of messages unread for a specific member in a specific channel. To do this I was hoping to use channel.getUnconsumedMessagesCount() as defined in the documentation.

myChannel.join()
  .then(function(c) {
    console.log('Joined channel ' + c.sid);
    return myChannel.getUnconsumedMessagesCount();
  })
  .then(m => {
    console.log('current count unread: ' + m);
  });

The unread count always return 0. To test, I do the following:

  1. user 2 submitting a message to myChannel in another chrome tab
  2. user 2 myChannel get's updated with the message from (1) via .on('messageAdded', [...])
  3. refresh user 1 chrome tab, and get getUnconsumedMessagesCount with value of 0
  4. If I call myChannel.getMessages() for user1, I see the message from user2

Initially I called .getUnconsumedMessagesCount() without doing join() first, I thought this could be the issue, but even with join still nothing.

guiomie
  • 4,988
  • 6
  • 37
  • 67
  • 1
    Sorry for the slow reply here. I can't see anything you've done wrong here, so it might be an issue on the Twilio side. I recommend getting in touch with [Twilio support](https://www.twilio.com/help/contact) and sending them the details, including the Chat Service SID that you're using. – philnash Feb 07 '17 at 15:14
  • Support ... oh no :/ Alright, thx. – guiomie Mar 08 '17 at 05:14

2 Answers2

5

before you can get UnconsumedMessagesCount you need manually set last read message (Send a Consumption Report)

something like this (JS)

 channel.getMessages(1).then(function (messages) {
    var last_message_index = messages.items[0].index;
    channel.updateLastConsumedMessageIndex(last_message_index);
 });
pymen
  • 5,737
  • 44
  • 35
4

I am having the same issue with the javascript SDK where it always returns 0.

I ended up doing the following which is working

const count = channel.getMessagesCount();
const unreadCount = channel.lastConsumedMessageIndex === null ? count : count - channel.lastConsumedMessageIndex - 1

I also noticed that using any of the functions to set the messages consumed can take up to a full minute before actually returning that information in the channel.lastConsumedMessageIndex

Philberg
  • 620
  • 7
  • 14