Skip to main content

Start, list, and cache conversations with XMTP

Most of the time, when interacting with the network, you'll want to do it through conversations. Conversations are between two wallets addresses.

Check if an address is on the network

First you need to check if the address you want to message is on the XMTP network. You can do this by calling client.canMessage with the address you want to message.

const isOnProdNetwork = await client.canMessage(
"0x3F11b27F323b62B159D2642964fa27C46C841897",
{ env: "production" },
);
  • Be sure to provide error messaging when a user enters an address in the To field and the address hasn't yet created an XMTP identity.

Start a new conversation

You can create a new conversation with any address activated on the XMTP network. To learn more about supported addresses, see Chains.

const newConversation = await xmtp.conversations.newConversation(
"0x937C0d4a6294cdfa575de17382c7076b579DC176",
);

Need to send a test message?

Message this XMTP message bot to get an immediate automated reply:

  • gm.xmtp.eth (0x937C0d4a6294cdfa575de17382c7076b579DC176)
🤖 Powered by Chainjet

List existing conversations

You can get a list of all conversations that have one or more messages.

These conversations include all conversations for a user regardless of which app created the conversation. This functionality provides the concept of an interoperable inbox, which enables a user to access all of their conversations in any app built with XMTP.

To provide a user-friendly cold start (first load), display a "Loading conversations" status message and a progress bar.

const allConversations = await xmtp.conversations.list();
// Say gm to everyone you've been chatting with
for (const conversation of allConversations) {
console.log(`Saying GM to ${conversation.peerAddress}`);
await conversation.send("gm");
}

Cache the conversation list

When running in a browser, conversations are cached in LocalStorage by default. Running client.conversations.list() will update that cache and persist the results to the browser's LocalStorage. The data stored in LocalStorage is encrypted and signed using the Keystore's identity key so that attackers cannot read the sensitive contents or tamper with them. Caching the conversation list can improve performance by up to 90%.

To disable this behavior, set the persistConversations client option to false.

const clientWithNoCache = await Client.create(wallet, {
persistConversations: false,
});

Serialize/Deserialize conversations

You can save a conversation object locally using its encodedContainer property. This returns a ConversationContainer object which conforms to Codable.

// Get a conversation
let conversation = try await client.conversations.newConversation(
with: "0x3F11b27F323b62B159D2642964fa27C46C841897")

// Get a container.
let container = conversation.encodedContainer

// Dump it to JSON
let encoder = JSONEncoder()
let data = try encoder.encode(container)

// Get it back from JSON
let decoder = JSONDecoder()
let containerAgain = try decoder.decode(ConversationContainer.self, from: data)

// Get an actual Conversation object like we had above
let decodedConversation = containerAgain.decode(with: client)
try await decodedConversation.send(text: "hi")

Was the information on this page helpful?
powered by XMTP