Twilio chat - unread messages count

Viewed 178

I'm looking for an efficient way to get and update count of unread messages in twilio chat. I've reviewed related Q&A here (like this), and the anser was about looping through the Channel's array and Message Consumption Horizon. Is it the one way to solve it? It's not a cheap approach from the point of view of perfomance.

The second question is about ability to update this count on the fly - so, is there a way to create a notification for user with event handler for incomming messages (not push notifications) to update counter of unread messages in real time?

thanks in advance

1 Answers

here is the way I've used to update the unread count real-time.

Refer Channel/Conversation: For twilio-chat consider channel & for twilio-conversation consider conversation.

Listen to the 'messageAdded' event of ConversationClient. So whenever a new message is added you can check below:

  1. If you have any conversation in focus: match conversation SID & reset the unread count to the newly added message's index.
  2. If there isn't any conversation in focus or conversation SID does not match - then find out the conversation with the message's conversation SID & increment its unread message count.

Hope this is helpful.

Related