Support replies in your app built with XMTP
Use the reply content type to support quote replies in your app. A reply is a method to directly respond to a specific message in a conversation. Users can select and reply to a particular message instead of sending a new one.
Use a local database for performance
Use a local database to store replies. This will enable your app to performantly display a reply with its referenced message when rendering message lists.
Install the package
npm i @xmtp/content-type-reply
In some SDKs, the
ReplyCodec
is already included in the SDK. If not, you can install the package using the following command:
Configure the content type
Send a reply
Once you've created a reply, you can send it. Replies are represented as objects with two keys:
-
reference
: ID of the message being replied to -
content
: String representation of the reply
// Assuming you have a conversation object and the ID of the message you're replying to
const replyContent = {
reply: {
reference: messageId, // ID of the message you're replying to
content: {
text: "This is a reply", // Content of the reply
},
},
};
await conversation.send(replyContent);
Receive the content type
if (message.contentTypeId === "xmtp.org/reply:1.0") {
const reply = message.content();
if (reply) {
const replyContent: ReplyContent = reply;
const replyContentType = replyContent.contentType;
const codec = client.codecRegistry[replyContentType];
const actualReplyContent = codec.decode(replyContent.content);
}
}
To handle unsupported content types, refer to the fallback section.
Display the reply
How you choose to display replies in your app is up to you. It might be useful to look at the user experience for replies in popular apps such as Telegram and Discord. For example, in Discord, users can reply to individual messages, and the reply provides a link to the original message.
Note that the user experience of replies in iMessage and Slack follows more of a threaded pattern, where messages display in logical groupings, or threads. This reply content type doesn't support the threaded pattern. If you'd like to request support for a threaded reply pattern, post an XIP idea.