Send and list messages with XMTP
The message payload can be a plain string, but you can configure custom content types. To learn more, see Content types.
Send messages
To send a message, the recipient must have already started their client at least once and consequently advertised their key bundle on the network.
You might want to consider optimistically sending messages.
- JavaScript
- Swift
- Dart
- Kotlin
- React
- React Native
const conversation = await xmtp.conversations.newConversation(
"0x3F11b27F323b62B159D2642964fa27C46C841897",
);
await conversation.send("Hello world");
let conversation = try await client.conversations.newConversation(
with: "0x3F11b27F323b62B159D2642964fa27C46C841897")
try await conversation.send(content: "Hello world")
var convo = await client.newConversation("0x...");
await client.sendMessage(convo, 'gm');
val conversation =
client.conversations.newConversation("0x3F11b27F323b62B159D2642964fa27C46C841897")
conversation.send(text = "Hello world")
import { useSendMessage } from "@xmtp/react-sdk";
import type { Conversation } from "@xmtp/react-sdk";
import { useCallback, useState } from "react";
export const SendMessage: React.FC<{ conversation: CachedConversation }> = ({
conversation,
}) => {
const [peerAddress, setPeerAddress] = useState("");
const [message, setMessage] = useState("");
const [isSending, setIsSending] = useState(false);
const sendMessage = useSendMessage();
const handleAddressChange = useCallback(
(e: ChangeEvent<HTMLInputElement>) => {
setPeerAddress(e.target.value);
},
[],
);
const handleMessageChange = useCallback(
(e: ChangeEvent<HTMLInputElement>) => {
setMessage(e.target.value);
},
[],
);
const handleSendMessage = useCallback(
async (e: React.FormEvent) => {
e.preventDefault();
if (peerAddress && isValidAddress(peerAddress) && message) {
setIsLoading(true);
await sendMessage(conversation, message);
setIsLoading(false);
}
},
[message, peerAddress, sendMessage],
);
return (
<form onSubmit={handleSendMessage}>
<input
name="addressInput"
type="text"
onChange={handleAddressChange}
disabled={isSending}
/>
<input
name="messageInput"
type="text"
onChange={handleMessageChange}
disabled={isSending}
/>
</form>
);
};
Optimistic sending with React
When a user sends a message with XMTP, they might experience a slight delay between sending the message and seeing their sent message display in their app UI.
This is because when a user sends a message, they typically have to wait for the XMTP network to finish processing the message before the app can display it in the UI.
The local-first architecture of the React SDK automatically includes optimistic sending to immediately display the sent message in the sender’s UI while processing the message in the background. This provides the user with immediate feedback and enables them to continue messaging without having to wait for their previous message to finish processing.
Messages that are in the sending state will have a true
value for their isSending
property.
Handle messages that fail to send with React
If a message fails to complete the sending process, you must provide an error state that alerts the user and enables them to either resend the message or cancel sending the message.
While in this unsent state, the message remains in its original location in the user’s conversation flow, with any newer sent and received messages displaying after it.
If the user chooses to resend the message, the message moves into the most recently sent message position in the conversation. Once it successfully sends, it remains in that position.
If the user chooses to cancel sending the message, the message is removed from the conversation flow.
Messages that fail to send will have the hasSendError
property set to true
.
Resend a failed message
Use the resendMessage
function from the useResendMessage
hook to resend a failed message.
const { resendMessage } = useResendMessage();
// resend the message
resendMessage(failedMessage);
const conversation = await xmtp.conversations.newConversation(
"0x3F11b27F323b62B159D2642964fa27C46C841897",
);
await conversation.send("Hello world");
List messages in a conversation
You can receive the complete message history in a conversation.
- JavaScript
- Swift
- Dart
- Kotlin
- React
- React Native
for (const conversation of await xmtp.conversations.list()) {
// All parameters are optional and can be omitted
const opts = {
// Only show messages from last 24 hours
startTime: new Date(new Date().setDate(new Date().getDate() - 1)),
endTime: new Date(),
};
const messagesInConversation = await conversation.messages(opts);
}
for conversation in client.conversations.list() {
let messagesInConversation = try await conversation.messages()
}
// Only show messages from the last 24 hours.
var messages = await alice.listMessages(convo,
start: DateTime.now().subtract(const Duration(hours: 24)));
for (conversation in client.conversations.list()) {
val messagesInConversation = conversation.messages()
}
import { useMessages } from "@xmtp/react-sdk";
import type { CachedConversation } from "@xmtp/react-sdk";
export const Messages: React.FC<{
conversation: CachedConversation;
}> = ({ conversation }) => {
const { error, messages, isLoading } = useMessages(conversation);
if (error) {
return "An error occurred while loading messages";
}
if (isLoading) {
return "Loading messages...";
}
return (
...
);
};
for (const conversation of await xmtp.conversations.list()) {
const messagesInConversation = await conversation.messages(before: new Date(
new Date().setDate(new Date().getDate() - 1)), after: new Date())
}
List messages in a conversation with pagination
If a conversation has a lot of messages, it's more performant to retrieve and process the messages page by page instead of handling all of the messages at once.
- JavaScript
- Swift
- Dart
- Kotlin
- React
- React Native
Call conversation.messagesPaginated()
, which will return an AsyncGenerator yielding one page of results at a time. conversation.messages()
uses this under the hood internally to gather all messages.
const conversation = await xmtp.conversations.newConversation(
"0x3F11b27F323b62B159D2642964fa27C46C841897",
);
for await (const page of conversation.messagesPaginated({ pageSize: 25 })) {
for (const msg of page) {
// Breaking from the outer loop will stop the client from requesting any further pages
if (msg.content === "gm") {
return;
}
console.log(msg.content);
}
}
Call conversation.messages(limit: Int, before: Date)
, which will return the specified number of messages sent before that time.
let conversation = try await client.conversations.newConversation(
with: "0x3F11b27F323b62B159D2642964fa27C46C841897")
let messages = try await conversation.messages(limit: 25)
let nextPage = try await conversation.messages(limit: 25, before: messages[0].sent)
Specify limit
and end
, which will return the specified number
of messages sent before that time.
var messages = await alice.listMessages(convo, limit: 10);
var nextPage = await alice.listMessages(
convo, limit: 10, end: messages.last.sentAt);
Call conversation.messages(limit: Int, before: Date)
, which will return the specified number of messages sent before that time.
val conversation =
client.conversations.newConversation("0x3F11b27F323b62B159D2642964fa27C46C841897")
val messages = conversation.messages(limit = 25)
val nextPage = conversation.messages(limit = 25, before = messages[0].sent)
import { useMessages } from "@xmtp/react-sdk";
const { error, isLoading, messages, next } = useMessages(
conversation,
options: {
limit: 20,
},
);
const handleClick = useCallback(() => {
// fetch next page of messages
next();
}, [next]);
return (
<button type="button" onClick={handleClick}>
Load more messages
</button>
);
const conversation = await xmtp.conversations.newConversation(
'0x3F11b27F323b62B159D2642964fa27C46C841897'
)
for await (const page of conversation.messages(limit: 25)) {
for (const msg of page) {
// Breaking from the outer loop will stop the client from requesting any further pages
if (msg.content === 'gm') {
return
}
console.log(msg.content)
}
}
Decode a single message
You can decode a single Envelope
from XMTP using the decode
method:
- Swift
- Kotlin
let conversation = try await client.conversations.newConversation(
with: "0x3F11b27F323b62B159D2642964fa27C46C841897")
// Assume this function returns an Envelope that contains a message for the above conversation
let envelope = getEnvelopeFromXMTP()
let decodedMessage = try conversation.decode(envelope)
val conversation =
client.conversations.newConversation("0x3F11b27F323b62B159D2642964fa27C46C841897")
// Assume this function returns an Envelope that contains a message for the above conversation
val envelope = getEnvelopeFromXMTP()
val decodedMessage = conversation.decode(envelope)