Skip to main content

Build group chat with XMTP

Status

info

At present, the JavaScript SDK lacks support for Group Chat functionalities. Nevertheless, for those looking to integrate backend features, the CLI provides a viable solution, as detailed in this repository. For practical application, an example implementation is available on Replit. To explore group functionalities further, refer to the comprehensive Token Gated Group Chat Tutorial.

Secure group chats are an important part of every messaging app. In this guide, we delve into the essentials of using XMTP for creating secure group chats. From the initial steps of starting a new group chat, listing and caching conversations for quick access, to advanced topics like managing group members and synchronizing message history data across devices.

caution

This project is in Alpha status and ready for you to experiment with. However, we do not recommend using Alpha software in production apps. Software in this status is likely to change based on feedback.

Create a group chat

Initiate a new group chat with a list of specified addresses. To create a group, the recipient must have already started their client at least once on the XMTP network.

const group = await client.conversations.newGroup(
[walletAddress1, walletAddress2],
// Set permissions for the group. Options include "creator_is_admin" where only the creator has admin rights, or "everyone_is_admin" where all members are admins.
{ permissions: "creator_is_admin" },
);

Send a message in a group chat

Send a message to an existing group chat. Refer to the Messages section for more details.

const group = await client.conversations.newGroup([
walletAddress1,
walletAddress2,
]);
// Send a message
await group.send("Hello, group!");

Synchronizing group conversations

XMTP's syncGroups brings the current data from the network and updates local DB, reflecting new groups or membership changes. Use syncGroups to:

  • After Signing In: Immediately update group conversation data.
  • Periodically: Keep data current based on your app's requirements.
  • After Receiving a Notification: Reflect changes in group membership prompted by notifications.
await client.conversations.syncGroups();

List group chat conversations

Retrieve all existing group chat conversations associated with the current XMTP client. Refer to the Conversations section for more details.

//First fetch new data from the network
await client.conversations.syncGroups();
//Get the updated group list
const groups = await client.conversations.listGroups();

Check if a group chat is active

The isActive property indicates whether the current user is still a participant in the group chat. If the group chat is not active for the user, it typically means the user has been removed from the group. Developers should use this status to adjust the user interface accordingly. If a group chat is not active for a user, the application should hide or disable functionalities such as sending messages, adding, or removing members. This ensures a good user experience and prevents actions that are not permissible due to the user's status in the group chat.

const isActive = await group.isActive();

Get group chat messages

To ensure your application has the latest group chat details, including member list and the most recent messages, it's crucial to periodically synchronize each group chat. This can be particularly important after joining a group, adding new members, or sending messages.

Group chats are currently per installation

As of now, group chats in XMTP are specific to each installation. This means that while you can access your group chat conversations across different devices, the historical messages within those chats might not automatically appear. Currently, each group chat's message history is tied to the device where it was initiated. As a result, there is no automatic syncing of message history across devices. When you sign in on a new device, you will see existing group chat conversations but will only receive new messages from that point forward. We are actively working on enhancing this feature to improve your experience with group conversations.

// Get group messages
await group.messages();

Use the sync() method on a group chat object to update its state with the latest information from the XMTP network.

Manage group chat members

You can list, add and remove members from a group chat. Only the creator of the group chat has the permissions to add or remove members. This restriction ensures that only authorized individuals can modify the participant list. Developers should design their application's user interface and functionality with this consideration in mind, ensuring that options to add or remove members are only available to the group's creator.

List group members

Retrieve a list of wallet addresses for all members in the group chat

const members = await group.memberAddresses();

Add group members

Add new members to an existing group chat using their wallet addresses.

await group.addMembers([walletAddress]);

Remove group members

Remove members from an existing group chat using their wallet addresses.

await group.removeMembers([walletAddress]);

Listen for new messages and updates

Streams enable real-time monitoring of new messages in a group chat as well as member management activities like adding and removing members. Here's how you can set up a stream for message updates. Refer to this section for more details on streams.

List for messages specific to a group chat

// Assuming `group` is an existing group chat object
const streamGroupMessages = async (group) => {
const cancelGroupMessageStream = await group.streamGroupMessages(
async (message) => {
console.log(`New message: ${message.content}`);
// Membership updates
if (message.contentTypeId === ContentTypes.GroupMembershipChange) {
const addresses = await group.memberAddresses();
// Get new members
console.log(addresses); // Example usage of addresses
}
},
);

// Use cancelGroupMessageStream() to stop listening to group updates
return cancelGroupMessageStream;
};

And for streaming all conversations, including individual and groups:

const streamAllGroupMessages = async (client) => {
const allConvos = [];
const cancelStreamAllGroupMessages =
await client.conversations.streamAllGroupMessages(async (message) => {
console.log(`New message: ${message.content}`);
});
// Use cancelStreamAllGroupMessages() to stop listening to all conversation updates
};

Listen for new group chats

Monitor the creation of new group chats.

// Listen for group chat updates
const streamGroups = async (client) => {
const groups = [];
const cancelStreamGroups = await client.conversations.streamGroups(
(group) => {
groups.push(group);
},
);

// Use cancelStreamGroups() to stop listening to group updates
};

And for streaming all conversations, including individual and groups:

const streamAllConversations = async (client) => {
const allConvos = [];
const cancelStreamAll = await client.conversations.streamAll(
(conversation) => {
allConvos.push(conversation);
},
);

// Use cancelStreamAll() to stop listening to all conversation updates
};

Note on conversations and messages in group chats

It's important to note that all the features and methods described in the Conversations and Messages documentation are fully applicable to group chats as well. This includes starting new conversations, checking if an address is on the network, sending messages, and listing existing conversations. XMTP's API design ensures that you can manage group chats with the same ease and flexibility as one-on-one conversations.

This means that whether you're working with individual conversations or group chats, the process for managing them remains consistent, allowing for a unified and streamlined development experience across different types of communication within XMTP.

Was the information on this page helpful?
powered by XMTP