Manage group chats with XMTP
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.
- React Native
- Kotlin
- Swift
- Dart
- JavaScript
- React
const group = await client.conversations.newGroup([
walletAddress1,
walletAddress2,
]);
val group = client.conversations.newGroup(listOf(walletAddress1,walletAddress2))
let group = try await client.conversations.newGroup(with: [walletAddress1, walletAddress2])
Code sample expected for Q2 2024
Code sample expected for Q2 2024
Code sample expected for Q2 2024
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.
- React Native
- Kotlin
- Swift
- Dart
- JavaScript
- React
const groups = await client.conversations.listGroups();
// 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)
// List all group chats for the current user
let groups = try await client.conversations.groups()
Code sample expected for Q2 2024
Code sample expected for Q2 2024
Code sample expected for Q2 2024
Send a message in a group chat
Send a message to an existing group chat. Refer to the Messages section for more details.
- React Native
- Kotlin
- Swift
- Dart
- JavaScript
- React
const group = await client.conversations.newGroup([
walletAddress1,
walletAddress2,
]);
// Send a message
await group.send("Hello, group!");
val group = client.conversations.newGroup(listOf(walletAddress1,walletAddress2))
//Send a message
group.send("Hello, group!")
let group = try await client.conversations.newGroup(with: [walletAddress1, walletAddress2])
//Send a message
try await group.send(content: "Hello, group!")
Code sample expected for Q2 2024
Code sample expected for Q2 2024
Code sample expected for Q2 2024
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
- React Native
- Kotlin
- Swift
- Dart
- JavaScript
- React
await group.sync();
const members = await group.memberAddresses();
group.sync()
val members = group.memberAddresses()
try await group.sync()
let members = group.members()
Code sample expected for Q2 2024
Code sample expected for Q2 2024
Code sample expected for Q2 2024
Add group members
Add new members to an existing group chat using their wallet addresses.
- React Native
- Kotlin
- Swift
- Dart
- JavaScript
- React
await group.addMembers([walletAddress]);
group.addMembers(listOf(walletAddress))
try await group.addMembers(addresses: [walletAddress])
Code sample expected for Q2 2024
Code sample expected for Q2 2024
Code sample expected for Q2 2024
Remove group members
Remove members from an existing group chat using their wallet addresses
- React Native
- Kotlin
- Swift
- Dart
- JavaScript
- React
await group.removeMembers([walletAddress]);
group.removeMembers(listOf(walletAddress))
try await group.removeMembers(addresses: [walletAddress])
Code sample expected for Q2 2024
Code sample expected for Q2 2024
Code sample expected for Q2 2024
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.
- Kotlin
- Swift
- React Native
- Dart
- JavaScript
- React
// 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}")
}
Code sample expected for Q2 2024
Code sample expected for Q2 2024
Code sample expected for Q2 2024
Code sample expected for Q2 2024
Code sample expected for Q2 2024
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.
- Kotlin
- Swift
- React Native
- Dart
- JavaScript
- React
// 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}")
}
Code sample expected for Q2 2024
Code sample expected for Q2 2024
Code sample expected for Q2 2024
Code sample expected for Q2 2024
Code sample expected for Q2 2024