Skip to main content

Support read receipts in your app built with XMTP

Status Status

Use the read receipt content type to support read receipts in your app. A read receipt is a timestamp that indicates when a message was read. It is sent as a message and can be used to calculate the time since the last message was read.

Open for feedback

You're welcome to provide feedback by commenting on the Proposal for read receipts content type XIP idea.

Configure the read receipt content type

npm i @xmtp/content-type-read-receipt

Import and register

import {
ContentTypeReadReceipt,
ReadReceiptCodec,
} from "@xmtp/content-type-read-receipt";
// Create the XMTP client
const xmtp = await Client.create(signer, { env: "dev" });
xmtp.registerCodec(new ReadReceiptCodec());

Send a read receipt

  • messageId: ID of the message that was read
  • timestamp: Timestamp the read receipt sent, in ISO 8601 format
const readReceipt: ReadReceipt = {};

Once you've created a read receipt, you can send it:

await conversation.messages.send({}, ContentTypeReadReceipt);

Receive a read receipt

Here's how you can receive a read receipt:

if (message.contentType.sameAs(ContentTypeReadReceipt)) {
// We've got a reply.
const timestamp = message.content.timestamp;
}

Use a read receipt

A read receipt timestamp shouldn't be displayed. Instead, use it to calculate the time since the last message was read. While iterating through messages, you can be sure that the last message was read at the timestamp of the read receipt if the string of the date is lower.

function checkReadMessages(messages, readReceipt) {
return messages.map((message) => {
return {
...message,
isRead: message.timestamp <= readTimestamp,
};
});
}

Was the information on this page helpful?
powered by XMTP