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
After importing the package, you can register the codec.
import { ContentTypeReply, ReplyCodec } from "@xmtp/content-type-reply";
// Create the XMTP client
const xmtp = await Client.create(signer, { env: "dev" });
xmtp.registerCodec(new ReplyCodec());
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
import { ContentTypeText } from "@xmtp/content-type-text";
import { ContentTypeReply } from "@xmtp/content-type-reply";
import type { Reply } from "@xmtp/content-type-reply";
const reply: Reply = {
reference: someMessageID,
contentType: ContentTypeText,
content: "I concur",
};
await conversation.send(reply, {
contentType: ContentTypeReply,
});
Receive the content type
if (message.contentType.sameAs(ContentTypeReply)) {
// We've got a reply.
const reply: Reply = message.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.