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.

val group = client.conversations.newGroup(listOf(walletAddress1,walletAddress2))

The maximum amount of addresses allowed is X.

List group chat conversations

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

// List all group chats for the current user
val groups = client.conversations.listGroups()

List all you conversation for both group and individual conversations using includeGroups

// Use existing list() method
val conversations = client.conversations.list(includeGroups = true)

Send a message in a group chat

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

val group = client.conversations.newGroup(listOf(walletAddress1,walletAddress2))
//Send a message
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

group.sync()
val members = group.memberAddresses()

Add group members

Add new members to an existing group chat using its wallet address.

group.addMembers(listOf(walletAddress))

Remove group members

Remove a member from an existing group chat using its wallet address

group.removeMembers(listOf(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