Skip to main content

Manage group chats with XMTP

Status

Secure group chats are an important part of every messaging app. Learn how to create and manage them with XMTP. Discover how XMTP can enhance your group communication experience.

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,
]);

The maximum amount of addresses allowed is 250.

List group chat conversations

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

const groups = await client.conversations.listGroups();

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!");

Manage group chat members

Here are the ways that you manage group chat members with XMTP.

List group members

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

await group.sync();
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 in a group chat

Streams allow real-time monitoring of new messages in a group chat. Here's how you can set up a stream for message updates. Refer to the Streams section for more details.

// Stream new messages in a group chat
val group = client.conversations.newGroup(listOf(walletAddress1, walletAddress2))
val messageStream = group.streamMessages()

// Collect from the Flow to receive messages
messageStream.collect { message ->
print("New message from ${message.senderAddress}: ${message.body}")
}

Listen for group chat updates

Monitor updates in group chats, including member management activities like adding and removing members as well as the creation of new group chats.

// Stream updates for all group conversations
val groupsStream = client.conversations.streamGroups()

groupsStream.collect { group ->
println("New or updated group: ${group.id}")
}

Keep your conversation list current by streaming updates for both group and individual conversations using includeGroups.

// Stream updates for all conversations, including individual and groups
val conversationStream = client.conversations.stream(includeGroups = true)

conversationStream.collect { conversation ->
println("New or updated conversation: ${conversation.id}")
}

Was the information on this page helpful?
powered by XMTP