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 ChatGroup, ChatGroupManager, and ChatGroupEventHandler classes for chat group management, which allows you to implement the following features:

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.

Add a user to a chat group

The logic of adding a user to a chat group varies according to the GroupStyle and inviteNeedConfirm settings when creating the chat group. For details, see Create a Chat Group.

The following code sample shows how to call addMembers to add a user to a chat group:

try {
  await ChatClient.getInstance.groupManager.addMembers(groupId, members);
} on ChatError catch (e) {
}

Remove a member from a chat group

Only the chat group owner and admins can call removeMembers to remove one or more members from a chat group. Once removed from the chat group, the member receives the ChatGroupEventHandler#onUserRemovedFromGroup callback, while all the other members receive the ChatGroupEventHandler#onMembersExitedFromGroup callback. Users can join the chat group again after being removed.

The following code sample shows how to remove a member from a chat group:

try {
  await ChatClient.getInstance.groupManager.removeMembers(groupId, members);
} on ChatError catch (e) {
}

Manage the chat group owner and admins

Transfer the chat group ownership

Only the chat group owner can call changeOwner to transfer the ownership to the specified chat group member. Once the ownership is transferred, the former chat group owner becomes a regular member, and the new owner receives the ChatGroupEventHandler#onOwnerChangedFromGroup callback.

The following code sample shows how to transfer the chat group ownership:

try {
  await ChatClient.getInstance.groupManager.changeOwner(groupId, newOwner);
} on ChatError catch (e) {
}

Add a chat group admin

Only the chat group owner can call addAdmin to add an admin. Once promoted to an admin, the new admin and the other chat group admins receive the ChatGroupEventHandler#onAdminAddedFromGroup callback.

The following code sample shows how to add a chat group admin:

try {
  ChatClient.getInstance.groupManager.addAdmin(groupId, memberId);
} on ChatError catch (e) {
}

Remove a chat group admin

Only the chat group owner can call removeAdmin to remove an admin. Once demoted to a regular member, the former admin and the other chat group admins receive the ChatGroupEventHandler#onAdminRemovedFromGroup callback.

The following code sample shows how to remove a chat group admin:

try {
  await ChatClient.getInstance.groupManager.removeAdmin(groupId, adminId);
} on ChatError catch (e) {
}

Manage the chat group blocklist

Add a member to the chat group blocklist

Only the chat group owner and admins can call blockMembers to add the specified member to the chat group blocklist. Once added to the blocklist, this member receives the ChatGroupEventHandler#onUserRemovedFromGroup callback, while all the other members receive the ChatGroupEventHandler#onMembersExitedFromGroup callback. After being added to blocklist, this user cannot send or receive messages in the chat group. They can no longer join the chat room until they are removed from the blocklist.

The following code sample shows how to add a member to the chat group blocklist:

try {
  await ChatClient.getInstance.groupManager.blockMembers(groupId, members);
} on ChatError catch (e) {
}

Remove a member from the chat group blocklist

Only the chat group owner and admins can call unblockMembers to remove the specified member from the chat group blocklist.

The following code sample shows how to remove a member from the chat group blocklist:

try {
  await ChatClient.getInstance.groupManager.unblockMembers(groupId, blockIds);
} on ChatError catch (e) {
}

Retrieve the chat group blocklist

Only the chat group owner and admins can call fetchBlockListFromServer to retrieve the chat group blocklist.

The following code sample shows how to retrieve the chat group blocklist:

try {
  await ChatClient.getInstance.groupManager.fetchBlockListFromServer(
    groupId,
    pageNum: pageNum,
    pageSize: pageSize,
  );
} on ChatError catch (e) {
}

Manage the chat group mute list

Add a member to the chat group mute list

Only the chat group owner and admins can call muteMembers to add the specified member to the chat group mute list. Once added to the mute list, this member and all the other chat group admins or owner receive the ChatGroupEventHandler#onMuteListAddedFromGroup callback. Once a chat group member is added to the chat group mute list, they can no longer send chat group messages, not even after being added to the chat group allow list.

The following code sample shows how to add a member to the chat group mute list:

// duration: The mute duration. If you pass `-1`, members are muted permanently.
try {
  await ChatClient.getInstance.groupManager.muteMembers(
    groupId,
    members,
  );
} on ChatError catch (e) {
}

Remove a member from the chat group mute list

Only the chat group owner and admins can call unMuteMembers to remove the specified member from the chat group mute list. Once removed from the chat group mute list, this member and all the other chat group admins or owner receive the ChatGroupEventHandler#onMuteListRemovedFromGroup callback.

The following code sample shows how to remove a member from the chat group mute list:

try {
  await ChatClient.getInstance.groupManager.unMuteMembers(
    groupId,
    members,
  );
} on ChatError catch (e) {
}

Retrieve the chat group mute list

Only the chat group owner and admins can call fetchMuteListFromServer to retrieve the chat group mute list from the server.

The following code sample shows how to retrieve the chat group mute dictionary:

try {
  await ChatClient.getInstance.groupManager.fetchMuteListFromServer(
    groupId,
    pageNum: pageNum,
    pageSize: pageSize,
  );
} on ChatError catch (e) {
}

Mute all the chat group members

Only the chat group owner and admins can call muteAllMembers to mute all the chat group members. All members in the group can receive the ChatGroupEventHandler#onAllMemberMuteStateChanged event.

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 unMuteAllMembers method to unmute all members in the chat group.

The following sample code shows how to mute all the chat group members:

try {
  await ChatClient.getInstance.groupManager.muteAllMembers(
    groupId,
  );
} on ChatError catch (e) {
}

Unmute all the chat group members

Only the chat group owner and admins can call unMuteAllMembers to unmute all the chat group members. All members in the group can receive the ChatGroupEventHandler#onAllMemberMuteStateChanged event.

The following sample code shows how to unmute all the chat group members:

try {
  await ChatClient.getInstance.groupManager.unMuteAllMembers(
    groupId,
  );
} on ChatError catch (e) {
}

Manage the chat group allow list

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

Add a member to the chat group allow list

Only the chat group owner and admins can call addAllowList to add the specified member to the chat group allow list. 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 group allow list.

The following code sample shows how to add a member to the chat group allow list:

try {
  await ChatClient.getInstance.groupManager.addAllowList(
    groupId,
    members,
  );
} on ChatError catch (e) {
}

Remove a member from the chat group allow list

Only the chat group owner and admins can call removeAllowList to remove the specified member from the chat group allow list.

The following code sample shows how to remove a member from the chat group allow list:

try {
  await ChatClient.getInstance.groupManager.removeAllowList(
    groupId,
    members,
  );
} on ChatError catch (e) {
}

Check whether a user is added to the allow list

All chat group members can call isMemberInAllowListFromServer to check whether they are added to the chat group allow list.

The following code sample shows how to check whether a user is on the chat group allow list:

try {
  bool check = await ChatClient.getInstance.groupManager.isMemberInAllowListFromServer(
    groupId,
  );
} on ChatError catch (e) {
}

Retrieve the chat group allow list

Only the chat group owner and admins can call fetchAllowListFromServer to retrieve the chat group allow list.

The following code sample shows how to retrieve the chat group allow list:

try {
  List? list =
      await ChatClient.getInstance.groupManager.fetchAllowListFromServer(
    groupId,
  );
} on ChatError catch (e) {
}

Listen for chat group events

For details, see Chat Group Events.