Search code examples
next.jstwiliotwilio-programmable-chat

How to see if a twilio message has been read?


I have a nextjs project and I integrated twilio programmable chat. It basically works. Next step is to add notifications and I have very big problems due to the not updated or lack of doc. I tried this guide for web push notifications but I gave it up because after the step7 I don't know what to do and can't find anything about it.

What I want to do now is to get the status of the messages and eventually update them once I read them. First of all is it possible to do it? I don't find anything about twilio web notifications on the internet.

For example if I want to get the messages of a specific room I do as follows:

const response = await getTwilioClient(token, unique_room_name);
const messages = await response.channel.getMessages(MESSAGES_LIMIT);

messages has the following shape:

{
  hasNextPage: boolean,
  hasPrevPage: boolean,
  items: Message[],
  nextPage: () => Message[],
  prevPage: () => Message[],
}

And Message looks like this:

enter image description here

So how can I see the status of a message?


Solution

  • Message read status is actually stored on the Member object. A Member is the object that represents a User in a Channel and it has a lastConsumedMessageIndex property which relates to the last message they read.

    In order to read the lastConsumedMessageIndex property, you need to set where the member has read up to, using the Channel methods advanceLastConsumedMessageIndex, setAllMessagesConsumed or setNoMessagesConsumed.

    I recommend you read the documentation on the Message Consumption Horizon and Read Status.

    As an extra note, I see you're asking questions about Twilio Chat, however Twilio Chat will be coming to the end of life in July 2022. We recommend that you migrate to the Twilio Conversations API instead.