Twilio Chat API, getUnconsumedMessagesCount is always 0

I am trying to get the number of unread messages for a specific member in a specific channel. For 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); }); 

An unread counter always returns 0. To check, I do the following:

  • user 2 sends a message to myChannel in another Chrome tab
  • user 2 myChannel is updated with a message from (1) via .on ('messageAdded', [...])
  • update user 1 chrome tab and get getUnconsumedMessagesCount with value 0
  • If I call myChannel.getMessages () for user1, I see a message from user2

At first, I called .getUnconsumedMessagesCount (), without first doing join (), I thought it might be a problem, but even with joining, it's still nothing.

+6
source share
2 answers

before you can get UnconsumedMessagesCount, you need to manually set the last message read ( 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); }); 
+3
source

I have the same problem with javascript SDK where it always returns 0.

I finished work that works

 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 up consumed messages may take up to a full minute before actually returning this information to channel.lastConsumedMessageIndex

+2
source

Source: https://habr.com/ru/post/1014534/


All Articles