Chat groups enable real-time messaging among multiple users.
This page shows how to use the Agora Chat SDK to manage the attributes of a chat group in your app.
The Agora Chat SDK provides the GroupManager
and Group
classes for chat group management, which allows you to implement the following features:
Before proceeding, ensure that you meet the following requirements:
This section describes how to call the APIs provided by the Agora Chat SDK to implement chat group features.
Add users to a chat group.
Whether a chat group is public or private, the chat group owner and chat group admins can add users to the chat group. If the allowinvites
parameter of the group type is set to true
, group members can also invite users to join the chat group.
Implement chat group invitations.
After a user is invited to join a chat group, the implementation logic varies based on the settings of the user:
inviteToJoin
callback. Once the user accepts the request and joins the group, the inviter receives the acceptInvite
callback and all group members receive the memberPresence
callback.directJoined
callback. In this case, the user automatically joins the group and all group members receive the memberPresence
callback.Remove chat group members from a chat group.
The chat group owner and chat group admins can remove chat group members from a chat group, whereas chat group members do not have this privilege. Once a group member is removed, this group member receives the removeMember
callback and all the other group members receive the memberAbsence
callback.
Refer to the following sample code to add and remove a user:
// Group members can call inviteUsersToGroup to add users to a chat group.
let option = {
users: ["user1", "user2"],
groupId: "groupId"
};
conn.inviteUsersToGroup(option).then(res => console.log(res))
// The chat group owner and chat group admins can call removeGroupMember to remove group members from a chat group.
let option = {
groupId: "groupId",
username: "username"
};
conn.removeGroupMember(option).then(res => console.log(res))
Transfer the chat group ownership.
The chat group owner can transfer ownership to the specified chat group member. Once ownership is transferred, the original chat group owner becomes a group member. All the other chat group members receive the changeOwner
callback.
Add chat group admins.
The chat group owner can add admins. Once added to the chat group admin list, the newly added admin and the other chat group admins receive the setAdmin
callback.
Remove chat group admins.
The chat group owner can remove admins. Once removed from the chat group admin list, the removed admin and the other chat group admins receive the removeGroupAdmin
callback.
Refer to the following sample code to manage chat group ownership and admin:
// The chat group owner can call changeGroupOwner to transfer ownership to the specified chat group member.
let option = {
groupId: "groupId",
newOwner: "username"
};
conn.changeGroupOwner(option).then(res => console.log(res))
// The chat group owner can call setGroupAdmin to add admins.
let option = {
groupId: "groupId",
username: "user"
};
conn.setGroupAdmin(option).then(res => console.log(res))
// The chat group owner can call removeGroupAdmin to remove admins.
let option = {
groupId: "groupId",
username: "user"
};
conn.removeGroupAdmin(option).then(res => console.log(res))
The chat group owner and chat group admins can add or remove the specified member to the chat group block list. Once a chat group member is added to the block list, this member cannot send or receive chat group messages, nor can this member join the chat group again.
Refer to the following sample code to manage the chat group block list:
// The chat group owner and admins call blockGroupMember to add the specified member to the chat group block list.
let option = {
groupId: "groupId",
usernames: ["user1", "user2"]
};
conn.blockGroupMember(option).then(res => console.log(res))
// The chat group owner and admins can call unblockGroupMembers to remove the specified member from the chat group block list.
// Once removed, the removed member can request to join the chat group again.
let option = {
groupId: "groupId",
username: ["user1", "user2"]
}
conn.unblockGroupMembers(option).then(res => console.log(res))
// The chat group owner and admins can call getGroupBlocklist to retrieve the chat group block list.
let option = {
groupId: "groupId",
};
conn.getGroupBlocklist(option).then(res => console.log(res))
The chat group owner and chat group admins can add or remove the specified member to the chat group mute list. Once a chat group member is added to the mute list, this member can no longer send chat group messages, not even after being added to the chat group allow list.
Refer to the following sample code to manage the chat group mute list:
// The chat group owner and admins can call muteGroupMember to add the specified member to the chat group mute list.
// The muted member and all the other chat group admins or owner receive the muteMember callback.
let option = {
groupId: "groupId",
username: "user",
muteDuration: 886400000 // The mute duration. Unit: millisecond.
};
conn.muteGroupMember(option).then(res => console.log(res))
// The chat group owner and admins can call unmuteGroupMember to remove the specified user from the chat group mute list.
// The unmuted member and all the other chat group admins or owner receive the unmuteMember callback.
let option = {
groupId: "groupId",
username: "user"
};
conn.unmuteGroupMember(option).then(res => console.log(res))
// The chat group owner or admin can call getGroupMutelist to retrieve the chat group mute list.
let option = {
groupId: "groupId"
};
conn.getGroupMutelist(option).then(res => console.log(res))
The chat group owner and chat group admins can mute or unmute all the chat group members. Once all the members are muted, only those in the chat group allow list can send messages in the chat group.
Refer to the following sample code to mute and unmute all the chat group members:
// The chat group owner or admin can call disableSendGroupMsg to mute all the chat group members.
// Once all the members are muted, these members receive the muteAllMembers callback.
let options = {
groupId: "groupId",
};
conn.disableSendGroupMsg(options).then(res => console.log(res))
// The chat group owner or admin can call enableSendGroupMsg to unmute all the chat group members.
// Once all the members are unmuted, these members receive the unmuteAllMembers callback.
let options = {
groupId: "groupId",
};
conn.enableSendGroupMsg(options).then(res => console.log(res))
Members in the chat group allow list can send chat group messages even when the chat group owner or admin has muted all chat group members. However, if a member is already in the chat group mute list, adding this member to the allow list does not take effect.
Refer to the following sample code to manage the chat group allow list:
// The chat group owner or admin can call addUsersToGroupAllowlist to add the specified member to the chat group allow list.
// Once the member is added, all the other chat group admins or owner receive the addUsersToGroupAllowlist callback.
[[AgoraChatClient sharedClient].groupManager addWhiteListMembers:members
let option = {
groupId: "groupId",
users: ["user1", "user2"]
};
conn.addUsersToGroupAllowlist(option).then(res => console.log(res));
// The chat group owner or admin can call removeAllowlistMember to remove the specified member from the chat group list.
// Once the member is removed, all the other chat group admins or owner receive the rmUserFromGroupWhiteList callback.
let option = {
groupId: "groupId",
userName: "user"
}
conn.removeAllowlistMember(option).then(res => console.log(res));
// Chat group members can call isInGroupAllowlist to check whether they are in the chat group allow list.
let option = {
groupId: "groupId",
userName: "user"
}
conn.isInGroupAllowlist(option).then(res => console.log(res));
// The chat group owner or admin can call getGroupAllowlist to retrieve the chat group allow list.
let options = {
groupId: "groupId"
}
conn.getGroupAllowlist(options).then(res => console.log(res));
For details, see Chat Group Events.