Manage chat groups

Updated

Introduces chat group functionalities.

Chat groups enable real-time messaging among multiple users.

This page shows how to use the Chat SDK to create and manage 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:

  • Create and destroy a chat group
  • Join and leave a chat group
  • Retrieve the chat group attributes
  • Retrieve the chat group member list
  • Retrieve the chat group list
  • Block and unblock a chat group
  • Listen for chat group events

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.

Create a chat group

Set ChatGroupStyle and inviteNeedConfirm before creating a chat group.

  1. Is the group public or private, and who can invite members (ChatGroupStyle):
  • PrivateOnlyOwnerInvite: A private group. Only the chat group owner and admins can add users to the chat group.
  • PrivateMemberCanInvite: A private group. All chat group members can add users to the chat group.
  • PublicJoinNeedApproval: A public group. The chat group owner and admins can add users, and users can send join requests to the chat group.
  • PublicOpenJoin: A public group. All users can join the chat group automatically without any need for approval from the chat group owner and admins.
  1. Does a group invitation require consent from an invitee to add them to the group (inviteNeedConfirm):
  • Yes (ChatGroupOptions#inviteNeedConfirm is set to true). After creating a group and sending group invitations, the subsequent logic varies based on whether an invitee automatically consents to the group invitation (autoAcceptGroupInvitation):
  • Yes (autoAcceptGroupInvitation is set to true). The invitee automatically joins the chat group and receives the ChatGroupEventHandler#onAutoAcceptInvitationFromGroup callback, the inviter receives the ChatGroupEventHandler#onInvitationAcceptedFromGroup and ChatGroupEventHandler#onMembersJoinedFromGroup callbacks, and the other chat group members receives the ChatGroupEventHandler#onMembersJoinedFromGroup callback.
  • No (autoAcceptGroupInvitation is set to false). The invitee receives the ChatGroupEventHandler#onInvitationReceivedFromGroup callback and chooses whether to join the chat group:
    • If the invitee accepts the group invitation, the inviter receives the ChatGroupEventHandler#onInvitationAcceptedFromGroup and ChatGroupEventHandler#onMembersJoinedFromGroup callbacks, and the other chat group members receive the ChatGroupEventHandler#onMembersJoinedFromGroup callback;
    • If the invitee declines the group invitation, the chat group owner receives the ChatGroupEventHandler#onInvitationDeclinedFromGroup callback.

  • No (ChatGroupOptions#inviteNeedConfirm is set to false). After creating a chat group and sending group invitations, an invitee is added to the chat group regardless of their autoAcceptGroupInvitation setting. The invitee receives the ChatGroupEventHandler#onAutoAcceptInvitationFromGroup callback, the inviter receives the ChatGroupEventHandler#onInvitationAcceptedFromGroup and ChatGroupEventHandler#onMembersJoinedFromGroup callbacks, and the other chat group members receive the ChatGroupEventHandler#onMembersJoinedFromGroup callback.

    Users can call createGroup to create a chat group and set the chat group attributes such as the chat group name, description, maximum number of members, and reason for creating the group, by specifying ChatGroupOptions.

    The following code sample shows how to create a chat group:

    ChatGroupOptions groupOptions = ChatGroupOptions(
      // The permission style of the chat group.
      style: ChatGroupStyle.PrivateMemberCanInvite,
      inviteNeedConfirm: true,
    );
    // The name of the chat group can be a maximum of 128 characters.
    String groupName = "newGroup";
    // The description of the chat group can be a maximum of 512 characters.
    String groupDesc = "group desc";
    try {
      await ChatClient.getInstance.groupManager.createGroup(
        groupName: groupName,
        desc: groupDesc,
        options: groupOptions,
      );
    } on ChatError catch (e) {
    }

Destroy a chat group

Only the chat group owner can call destroyGroup to disband a chat group. Once a chat group is disbanded, all chat group members receive the onGroupDestroyed callback and are immediately removed from the chat group.

Once a chat group is destroyed, all chat group data is deleted from the local database and memory.

The following code sample shows how to destroy a chat group:

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

Join a chat group

The logic of joining a chat group varies according to the GroupStyle setting you choose when creating the chat group:

  • If the GroupStyle is set to PublicOpenJoin, all users can join the chat group without the consent from the chat group owner and admins. Once a user joins a chat group, all chat group members receive the ChatGroupEventHandler#onMembersJoinedFromGroup callback;
  • If the GroupStyle is set to PublicJoinNeedApproval, users can send join requests to the chat group. The chat group owner and chat group admins receive the ChatGroupEventHandler#onRequestToJoinReceivedFromGroup callback and choose whether to accept the join request:
    • If the request is accepted, the user joins the chat group and receives the ChatGroupEventHandler#onRequestToJoinAcceptedFromGroup callback, while all the other chat group members receive the ChatGroupEventHandler#onMembersJoinedFromGroup callback.
    • If the request is declined, the user receives the ChatGroupEventHandler#onRequestToJoinDeclinedFromGroup callback.
Users can only request to join public groups; private groups do not allow join requests.

Users can refer to the following steps to join a chat group:

  1. Call fetchPublicGroupsFromServer to retrieve the list of public groups from the server, and locate the ID of the chat group that you want to join.

  2. Call joinPublicGroup to pass in the chat group ID and request to join the specified chat group.

The following code sample shows how to join a chat group:

// Retrieve the list of public groups with pagination.
try {
  ChatCursorResult<ChatGroupInfo> result =
      await ChatClient.getInstance.groupManager.fetchPublicGroupsFromServer();
} on ChatError catch (e) {
}
// Request to join the specified chat group.
try {
  await ChatClient.getInstance.groupManager.joinPublicGroup(groupId);
} on ChatError catch (e) {
}

Leave a chat group

Chat group members can call leaveGroup to leave the specified chat group, whereas the chat group owner cannot perform this operation. Once a member leaves a chat group, all the other chat group members receive the ChatGroupEventHandler#onMembersExitedFromGroup callback.

The following code sample shows how to leave a chat group:

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

Retrieve the attributes of a chat group

All chat group members can call getGroupWithId to retrieve the chat group attributes from memory. The attributes contain the chat group ID, name, description, owner, announcements, number of members, admin list, and whether to mute all members.

All chat group members can also call fetchGroupInfoFromServer to retrieve the chat group attributes from the server. The attributes contain the chat group ID, name, description, owner, announcements, number of members, admin list, and whether to mute all members.

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

// Retrieve the chat group attributes from memory.
try {
  ChatGroup? group = await ChatClient.getInstance.groupManager.getGroupWithId(groupId);
} on ChatError catch (e) {
}
// Retrieve the chat group attributes from the server.
try {
  ChatGroup group = await ChatClient.getInstance.groupManager.fetchGroupInfoFromServer(groupId);
} on ChatError catch (e) {
}

Retrieve the chat group member list

All chat group members can call fetchMemberListFromServer to retrieve the chat group member list from the server with pagination.

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

// The ID of the chat group.
try {
  ChatCursorResult<String> result =
      await ChatClient.getInstance.groupManager.fetchMemberListFromServer(
    groupId,
  );
} on ChatError catch (e) {
}

Since SDK v1.4.0, you can call fetchGroupMembersInfo to retrieve a member list in which each entry includes the member's user ID, role, and the time they joined the group:

try {
  ChatCursorResult<GroupMemberInfo> result =
      await ChatClient.getInstance.groupManager.fetchGroupMembersInfo(
    groupId: groupId,
    cursor: cursor,
    limit: limit,
  );
} on ChatError catch (e) {
}

Retrieve the chat group list

  • Users can call fetchJoinedGroupsFromServer to retrieve the groups they have joined and created from the server.

    try {
      List<ChatGroup> list =
          await ChatClient.getInstance.groupManager.fetchJoinedGroupsFromServer();
    } on ChatError catch (e) {
    }
  • Users can call getJoinedGroups to retrieve the joined chat group list from the local database. To ensure the accuracy of results, retrieve the joined chat group list from the server first.

    try {
      List<ChatGroup> list =
          await ChatClient.getInstance.groupManager.getJoinedGroups();
    } on ChatError catch (e) {
    }
  • Users can also call fetchPublicGroupsFromServer to retrieve public chat group list from the server with pagination:

    try {
      ChatCursorResult<ChatGroupInfo> result =
          await ChatClient.getInstance.groupManager.fetchPublicGroupsFromServer(
        // The maximum number of chat groups to retrieve with pagination.
        pageSize: pageSize,
        // The page number from which to start getting data.
        cursor: cursor,
      );
    } on ChatError catch (e) {
    }

Retrieve the number of groups joined by the current user

Users can call fetchJoinedGroupCount to retrieve the number of groups they have joined directly from the server. The limit on the number of groups a user can join is determined by the pricing package. For more details, please refer to the product pricing.

try {
  int count = await ChatClient.getInstance.groupManager.fetchJoinedGroupCount();
} on ChatError catch (e) {
  debugPrint('fetchJoinedGroupCount error: ${e.code} ${e.description}');
}

Block and unblock a chat group

Block a chat group

All chat group members can call blockGroup to block a chat group. Once a member blocks a chat group, this member can no longer receive messages from the chat group.

The following code sample shows how to block a chat group:

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

Unblock a chat group

All chat group members can call unblockGroup to unblock a chat group.

The following code sample shows how to unblock a chat group:

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

Check whether a user blocks a chat group

All chat group members can call fetchGroupInfoFromServer to check whether they block a chat group according to the ChatGroup#messageBlocked field.

The following code sample shows how to check whether a user blocks a chat group:

try {
  ChatGroup group = await ChatClient.getInstance.groupManager
      .fetchGroupInfoFromServer(groupId);
  // Check whether a user blocks a chat group.
  if (group.messageBlocked == true) {
  }
} on ChatError catch (e) {
}

Listen for chat group events

To monitor the chat group events, users can listen for the callbacks in the ChatGroupEventHandler class and add app logics accordingly. If a user wants to stop listening for the callbacks, make sure that the user removes the listener to prevent memory leakage.

Refer to the following code sample to listen for chat group events:

    /// Add a group event listener.
    ChatClient.getInstance.groupManager.addEventHandler(
      'UNIQUE_HANDLER_ID',
      ChatGroupEventHandler(
        /// Occurs when a member is added to the chat group admin list.
        onAdminAddedFromGroup: (groupId, admin) {},

        /// Occurs when an admin is removed from the chat group admin list.
        onAdminRemovedFromGroup: (groupId, admin) {},

        /// Occurs when all chat group members are muted or unmuted.
        onAllGroupMemberMuteStateChanged: (groupId, isAllMuted) {},

        /// Occurs when a member is added to the chat group allow list.
        onAllowListAddedFromGroup: (groupId, members) {},

        /// Occurs when a member is removed from the chat group allow list.
        onAllowListRemovedFromGroup: (groupId, members) {},

        /// Occurs when a member updates the chat group announcement.
        onAnnouncementChangedFromGroup: (groupId, announcement) {},

        /// Occurs when custom attributes of a group member are updated.
        onAttributesChangedOfGroupMember: (groupId, userId, attributes, operatorId) {},

        /// Occurs when an invitee accepts a group invitation automatically.
        onAutoAcceptInvitationFromGroup: (groupId, inviter, inviteMessage) {},

        /// Occurs when the group function is disabled or enabled.
        onDisableChanged: (groupId, isDisable) {},

        /// Occurs when the chat group owner disbands a chat group.
        onGroupDestroyed: (groupId, groupName) {},

        /// Occurs when an invitee accepts a group invitation.
        onInvitationAcceptedFromGroup: (groupId, invitee, reason) {},

        /// Occurs when an invitee declines a group invitation.
        onInvitationDeclinedFromGroup: (groupId, invitee, reason) {},

        /// Occurs when an invitee receives a group invitation.
        onInvitationReceivedFromGroup: (groupId, groupName, inviter, reason) {},

        /// Occurs when one or more members leave a chat group. All group members except those who left receive this callback.
        /// Available since SDK v1.4.0.
        onMembersExitedFromGroup: (groupId, userIds) {},

        /// Occurs when a member leaves a chat group.
        /// Deprecated since SDK v1.4.0. Use onMembersExitedFromGroup instead.
        onMemberExitedFromGroup: (groupId, member) {},

        /// Occurs when one or more members join a chat group. All group members except the new members receive this callback.
        /// Available since SDK v1.4.0.
        onMembersJoinedFromGroup: (groupId, userIds) {},

        /// Occurs when a user joins a chat group.
        /// Deprecated since SDK v1.4.0. Use onMembersJoinedFromGroup instead.
        onMemberJoinedFromGroup: (groupId, member) {},

        /// Occurs when a member is added to the chat group mute list.
        onMuteListAddedFromGroup: (groupId, mutes, muteExpire) {},

        /// Occurs when a member is removed from the chat group mute list.
        onMuteListRemovedFromGroup: (groupId, mutes) {},

        /// Occurs when the chat group owner is changed.
        onOwnerChangedFromGroup: (groupId, newOwner, oldOwner) {},

        /// Occurs when a join request is accepted.
        onRequestToJoinAcceptedFromGroup: (groupId, groupName, accepter) {},

        /// Occurs when a join request is declined.
        onRequestToJoinDeclinedFromGroup: (groupId, groupName, decliner, reason, applicant) {},

        /// Occurs when a join request is received.
        onRequestToJoinReceivedFromGroup: (groupId, groupName, applicant, reason) {},

        /// Occurs when a member uploads a chat group shared file.
        onSharedFileAddedFromGroup: (groupId, sharedFile) {},

        /// Occurs when a member deletes a chat group shared file.
        onSharedFileDeletedFromGroup: (groupId, fileId) {},

        /// Occurs when the specifications of a chat group is changed.
        onSpecificationDidUpdate: (group) {},

        /// Occurs when a member is removed from a chat group.
        onUserRemovedFromGroup: (groupId, groupName) {},
      ),
    );
    //...
    /// Removes a group event listener.
    ChatClient.getInstance.groupManager.removeEventHandler('UNIQUE_HANDLER_ID');