Skip to content

Build a chat inbox

Create or build a client

Create an account SigningKey

This code defines two functions that convert different types of Ethereum accounts—Externally Owned Accounts (EOAs) and Smart Contract Wallets (SCWs)—into a unified Signer interface. This ensures that both account types conform to a common interface for message signing and deriving shared secrets as per MLS (Message Layer Security) requirements.

  • For an EOA, the convertEOAToSigner function creates a signer that can get the account address and sign messages and has placeholder methods for wallet type, chain ID, and block number.

    Web
    SNIPPET FROM RY
  • For an SCW, the convertSCWToSigner function similarly creates a signer but includes specific implementations for wallet type and chain ID, and an optional block number computation.

    Web
    SNIPPET FROM RY

Create an XMTP client

Create an XMTP MLS client that can use the signing capabilities provided by the SigningKey parameter. This SigningKey links the client to the appropriate EOA or SCW.

Web
SNIPPET FROM RY

Build an existing client

Build, or resume, an existing client that's logged in and has an existing local database.

Web
SNIPPET FROM RY

Check if an address is reachable

The first step to creating a conversation is to verify that participants’ addresses are reachable on XMTP. The canGroupMessage method checks each address’ compatibility, returning a response indicating whether each address can receive messages.

Once you have the verified addresses, you can create a new conversation, whether it's a group chat or direct message (DM).

Web
SNIPPET FROM RY

Create a conversation

Create a new group chat

Once you have the verified addresses, create a new group chat:

Web
SNIPPET FROM RY

Create a new DM

Once you have the verified addresses, create a new DM:

Web
SNIPPET FROM RY

List conversations and messages

List new group chats or DMs

Get any new group chats or DMs from the network:

Web
SNIPPET FROM RY

List new messages

Get new messages from the network for all existing group chats and DMs in the local database:

Web
SNIPPET FROM RY

List existing group chats or DMs

Get a list of existing group chats or DMs in the local database, ordered either by createdAt date or lastMessage.

Web
SNIPPET FROM RY

Stream conversations and messages

Stream all group chats and DMs

Listens to the network for new group chats and DMs. Whenever a new conversation starts, it triggers the provided callback function with a ConversationContainer object. This allows the client to immediately respond to any new group chats or DMs initiated by other users.

Web
SNIPPET FROM RY

Stream all group chat and DM messages

Listens to the network for new messages within all active group chats and DMs. Whenever a new message is sent to any of these conversations, the callback is triggered with a DecodedMessage object. This keeps the inbox up to date by streaming in messages as they arrive.

Web
SNIPPET FROM RY

Helper methods and class interfaces

Conversation helper methods

Use these helper methods to quickly locate and access specific conversations—whether by ID, topic, group ID, or DM address—returning the appropriate ConversationContainer, group, or DM object.

Web
SNIPPET FROM RY

ConversationContainer interface

Serves as a unified structure for managing both group chats and DMs. It provides a consistent set of properties and methods to seamlessly handle various conversation types.

Group class

Represents a group chat conversation, providing methods to manage group-specific functionalities such as sending messages, synchronizing state, and handling group membership.

Dm class

Represents a DM conversation, providing methods to manage one-on-one communications, such as sending messages, synchronizing state, and handling message streams.