Manage chat group members

Updated

Introduces chat group functionalities.

Chat groups enable real-time messaging among multiple users.

This page shows how to use the Chat SDK to manage the members of a chat group in your app.

Understand the tech

The Chat SDK provides the GroupManager and Group classes for chat group management, which allows you to implement the following features:

  • Add and remove users from a chat group
  • Manage the owner and admins of a chat group
  • Manage the blocklist of a chat group
  • Manage the mute list of a chat group
  • Mute and unmute all the chat group members
  • Manage the allow list of a chat group

Prerequisites

Before proceeding, ensure that you meet the following requirements:

  • You have initialized the Chat SDK. For details, see SDK quickstart.
  • You understand the call frequency limits of the Chat APIs supported by different pricing plans as described in Limitations.
  • You understand the number of chat groups and chat group members supported by different pricing plans as described in Pricing Plan Details.

Implementation

This section describes how to call the APIs provided by the Chat SDK to implement chat group features.

Manage chat group members

  1. 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.

  2. Implement chat group invitations. After the user is invited to join a group, the implementation logic varies based on the setting of the group option inviteNeedConfirm:

    • If inviteNeedConfirm is set to true, there are two cases:

      • The user calls acceptGroupJoinRequest to accept the group invitation. After the user successfully joins the group, the inviter receives the acceptInvite callback and all group members receive the memberPresence callback.
      • The user calls rejectGroupJoinRequest to decline the group invitation. In this case, the inviter receives the rejectInvite callback.
    • If inviteNeedConfirm is set to false, the user is directly added to the chat group without confirming the group invitation. All group members receive the memberPresence callback.

  3. 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 removed from the group, the 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.
const options = {
  users: ["user1", "user2"],
  groupId: "groupId",
};
chatClient.inviteUsersToGroup(options).then((res) => console.log(res));

// The chat group owner and chat group admins can call removeGroupMember to remove a group member from a chat group.
const options = {
  groupId: "groupId",
  username: "username",
};
chatClient.removeGroupMember(options).then((res) => console.log(res));
// The chat group owner and chat group admins can call removeGroupMembers to remove members from a chat group.
chatClient.removeGroupMembers({
  groupId: "groupId",
  users: ["user1", "user2"],
});

Manage chat group ownership and admin

  1. 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. The new owner receives the changeOwner callback.

  2. 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.

  3. 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.
const options = {
  groupId: "groupId",
  newOwner: "username",
};
chatClient.changeGroupOwner(options).then((res) => console.log(res));

// The chat group owner can call setGroupAdmin to add admins.
const options = {
  groupId: "groupId",
  username: "user",
};
chatClient.setGroupAdmin(options).then((res) => console.log(res));

// The chat group owner can call removeGroupAdmin to remove admins.
const options = {
  groupId: "groupId",
  username: "user",
};
chatClient.removeGroupAdmin(options).then((res) => console.log(res));

Manage the chat group blocklist

The chat group owner and chat group admins can add or remove the specified member to the chat group blocklist. Once a chat group member is added to the blocklist, 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 blocklist:

// The chat group owner and admins call blockGroupMember to add the specified member to the chat group blocklist.
const options = {
  groupId: "groupId",
  usernames: ["user1", "user2"],
};
chatClient.blockGroupMember(options).then((res) => console.log(res));

// The chat group owner and admins can call unblockGroupMembers to remove the specified member from the chat group blocklist.
// Once removed, the removed member can request to join the chat group again.
const options = {
  groupId: "groupId",
  username: ["user1", "user2"],
};
chatClient.unblockGroupMembers(options).then((res) => console.log(res));

// The chat group owner and admins can call getGroupBlocklist to retrieve the chat group blocklist.
const options = {
  groupId: "groupId",
};
chatClient.getGroupBlocklist(options).then((res) => console.log(res));

Manage the chat group mute list

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.
// muteDuration: The mute duration. If you pass `-1`, members are muted permanently.
const options = {
    groupId: "groupId"
    username: "user",
    muteDuration: 886400000 // The mute duration. Unit: millisecond.
};
chatClient.muteGroupMember(options).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.
const options = {
    groupId: "groupId",
    username: "user"
};
chatClient.unmuteGroupMember(options).then(res => console.log(res))

// The chat group owner or admin can call getGroupMutelist to retrieve the chat group mute list.
const options = {
    groupId: "groupId"
};
chatClient.getGroupMutelist(options).then(res => console.log(res))

Mute and unmute all the chat group members

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.

Unlike muting a chat group member, this kind of mute does not expire automatically after a certain period and you need to call the enableSendGroupMsg method to unmute all members 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.
const options = {
  groupId: "groupId",
};
chatClient.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.
const options = {
  groupId: "groupId",
};
chatClient.enableSendGroupMsg(options).then((res) => console.log(res));

Manage the chat group allow list

The chat group owner and admins are added to the chat group allow list by default.

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, this member still cannot send messages in the group even after being added to the allow list.

Refer to the following sample code to manage the chat group allow list:

// The chat group owner or admin can call addUserToAllowlist 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.
const options = {
  groupId: "groupId",
  users: ["user1", "user2"],
};
chatClient.addUsersToGroupAllowlist(options).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.
const options = {
  groupId: "groupId",
  userName: "user",
};
chatClient.removeAllowlistMember(options).then((res) => console.log(res));

// Chat group members can call isInGroupAllowlist to check whether they are in the chat group allow list.
const option = {
  groupId: "groupId",
  userName: "user",
};
chatClient.isInGroupAllowlist(option).then((res) => console.log(res));

// The chat group owner or admin can call getGroupAllowlist to retrieve the chat group allow list.
const options = {
  groupId: "groupId",
};
chatClient.getGroupAllowlist(options).then((res) => console.log(res));

Listen for chat group events

For details, see Chat Group Events.