In other words, as a recipient, how do I inform the sender that I’ve received a message?You can mark the messages for a particular conversation as read using the markAsDelivered() method. This method takes the below parameters as input:
Parameter
Information
messageId
The ID of the message above which all the messages for a particular conversation are to be marked as read.
receiverId
In case of one to one conversation message’s sender UID will be the receipt’s receiver Id. In case of group conversation message’s receiver Id will be the receipt’s receiver Id.
receiverType
Type of the receiver. Could be either of the two values( user or group).
senderId
The UID of the sender of the message.
Messages for both user & group conversations can be marked as read using this method.Ideally, you would like to mark all the messages as delivered for any conversation when the user opens the chat window for that conversation. This includes two scenarios:
When the list of messages for the conversation is fetched: In this case you need to obtain the last message in the list of messages and pass the message ID of that message to the markAsDelivered() method.
When the user is on the chat window and a real-time message is received: In this case you need to obtain the message ID of the message and pass it to the markAsDelivered() method.
User
// Mark as delivered/read (pass message object)CometChat.markAsDelivered(message);CometChat.markAsRead(message);// Mark entire conversationCometChat.markConversationAsRead("UID", "user");// Listen for receipt eventsCometChat.addMessageListener("LISTENER_ID", new CometChat.MessageListener({onMessagesDelivered: (receipt) => { },onMessagesRead: (receipt) => { },onMessagesDeliveredToAll: (receipt) => { }, // Groups onlyonMessagesReadByAll: (receipt) => { } // Groups only}));
Delivery and read receipts track whether messages have been delivered to and read by recipients.
Register a MessageListener to receive delivery and read receipt events.
Callback
Description
onMessagesDelivered
Message delivered to a user
onMessagesRead
Message read by a user
onMessagesDeliveredToAll
Group message delivered to all members
onMessagesReadByAll
Group message read by all members
TypeScript
JavaScript
let listenerID: string = "UNIQUE_LISTENER_ID";CometChat.addMessageListener(listenerID,new CometChat.MessageListener({onMessagesDelivered: (messageReceipt: CometChat.MessageReceipt) => {console.log("Message delivered:", messageReceipt);},onMessagesRead: (messageReceipt: CometChat.MessageReceipt) => {console.log("Message read:", messageReceipt);},onMessagesDeliveredToAll: (messageReceipt: CometChat.MessageReceipt) => {console.log("Message delivered to all group members:", messageReceipt);},onMessagesReadByAll: (messageReceipt: CometChat.MessageReceipt) => {console.log("Message read by all group members:", messageReceipt);}}));
let listenerID = "UNIQUE_LISTENER_ID";CometChat.addMessageListener( listenerID, new CometChat.MessageListener({ onMessagesDelivered: (messageReceipt) => { console.log("Message delivered:", messageReceipt); }, onMessagesRead: (messageReceipt) => { console.log("Message read:", messageReceipt); }, onMessagesDeliveredToAll: (messageReceipt) => { console.log("Message delivered to all group members:", messageReceipt); }, onMessagesReadByAll: (messageReceipt) => { console.log("Message read by all group members:", messageReceipt); } }));
You will receive events in the form of MessageReceipt objects. The message receipt contains the below parameters:
Parameter
Information
messageId
The Id of the message prior to which all the messages for that particular conversation have been marked as read.
sender
User object containing the details of the user who has marked the message as read. System User for deliveredToAll & readByAll events.
receiverId
Id of the receiver whose conversation has been marked as read.
receiverType
type of the receiver (user/group)
receiptType
Type of the receipt (read/delivered)
deliveredAt
The timestamp of the time when the message was delivered. This will only be present if the receiptType is delivered.
readAt
The timestamp of the time when the message was read. This will only be present when the receiptType is read.
The markAsDelivered() and markAsRead() methods are fire-and-forget — they do not return a MessageReceipt object. Use the listener callbacks above to receive delivery and read confirmations.You will receive a list of MessageReceipt objects.
The following features will be available only if the Enhanced Messaging Status feature is enabled for your app.
onMessagesDeliveredToAll event,
onMessagesReadByAll event,
deliveredAt field in a group message,
readAt field in a group message.
markMessageAsUnread method.
Always remove listeners when no longer needed to prevent memory leaks.
Use getMessageReceipts() to fetch delivery and read receipts for a specific message. Useful for group messages to see which members have received/read the message.
TypeScript
JavaScript
let messageId: number = 123;CometChat.getMessageReceipts(messageId).then((receipts: CometChat.MessageReceipt[]) => {console.log("Message receipts:", receipts);},(error: CometChat.CometChatException) => {console.log("Error fetching receipts:", error);});
When fetching messages, each message object includes deliveredAt and readAt timestamps indicating when the message was delivered and read.
let deliveredAt = message.getDeliveredAt();let readAt = message.getReadAt();
The following features require Enhanced Messaging Status to be enabled for
your app: - onMessagesDeliveredToAll event - onMessagesReadByAll event -
deliveredAt field in group messages - readAt field in group messages -
markMessageAsUnread() method
Use markAsUnread() to mark a message as unread. This is useful for “mark as unread” functionality in conversation lists. Pass a message object (TextMessage, MediaMessage, or CustomMessage).