Manage chat groups
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 Group
, IGroupManager
, and IGroupManagerDelegate
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 GroupStyle
and inviteNeedConfirm
before creating a chat group.
- Is the group public or private, and who can invite members (
GroupStyle
):
PrivateOnlyOwnerInvite
: A private group. Only the inviter 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 inviter 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 inviter and admins.
- Does a group invitation require consent from an invitee to add them to the group (
inviteNeedConfirm
):
- Yes (
option.InviteNeedConfirm
is set totrue
). 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 totrue
). The invitee automatically joins the chat group and receives theIGroupManagerDelegate#OnAutoAcceptInvitationFromGroup
callback, the inviter receives theIGroupManagerDelegate#OnInvitationAcceptedFromGroup
andIGroupManagerDelegate#OnMemberJoinedFromGroup
callbacks, and all chat group members receive theIGroupManagerDelegate#OnMemberJoinedFromGroup
callback. - No (
AutoAcceptGroupInvitation
is set tofalse
). The invitee receives theIGroupManagerDelegate#OnInvitationReceivedFromGroup
callback and chooses whether to join the chat group:- If the invitee accepts the group invitation, the inviter receives the
IGroupManagerDelegate#OnInvitationAcceptedFromGroup
andIGroupManagerDelegate#OnMemberJoinedFromGroup
callbacks and all chat group members receive theIGroupManagerDelegate#OnMemberJoinedFromGroup
callback; - If the invitee declines the group invitation, the inviter receives the
IGroupManagerDelegate#OnInvitationDeclinedFromGroup
callback.
- If the invitee accepts the group invitation, the inviter receives the
- Yes (
- No (
option.InviteNeedConfirm
is set tofalse
). After creating a chat group and sending group invitations, an invitee is added to the chat group regardless of theirIsAutoAcceptGroupInvitation
setting. The invitee receives theIGroupManagerDelegate#OnAutoAcceptInvitationFromGroup
callback, the inviter receives theIGroupManagerDelegate#OnInvitationAcceptedFromGroup
andIGroupManagerDelegate#OnMemberJoinedFromGroup
callbacks, and all chat group members receive theIGroupManagerDelegate#OnMemberJoinedFromGroup
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 GroupOptions
.
The following code sample shows how to create a chat group:
Destroy a chat group
Only the inviter can call DestroyGroup
to disband a chat group. Once a chat group is disbanded, all chat group members receive the OnDestroyedFromGroup
callback and are immediately removed from the chat group.
The following code sample shows how to destroy a chat group:
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 toPublicOpenJoin
, all users can join the chat group without the consent from the inviter and admins. Once a user joins a chat group, all chat group members receive theIGroupManagerDelegate#OnMemberJoinedFromGroup
callback; - If the
GroupStyle
is set toPublicJoinNeedApproval
, users can send join requests to the chat group. The inviter and chat group admins receive theIGroupManagerDelegate#OnRequestToJoinReceivedFromGroup
callback and choose whether to approve the join request:- If the request is accepted, the user joins the chat group and receives the
IGroupManagerDelegate#OnRequestToJoinAcceptedFromGroup
callback, while all all chat group members receive theIGroupManagerDelegate#OnMemberJoinedFromGroup
callback. - If the request is declined, the user receives the
IGroupManagerDelegate#OnRequestToJoinDeclinedFromGroup
callback.
- If the request is accepted, the user joins the chat group and receives the
Users can refer to the following steps to join a chat group:
-
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. -
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:
Leave a chat group
Chat group members can call LeaveGroup
to leave the specified chat group, whereas the inviter cannot perform this operation. Once a member leaves a chat group, all all chat group members receive the IGroupManagerDelegate#OnMemberExitedFromGroup
callback.
The following code sample shows how to leave a chat group:
Retrieve the attributes of a chat group
All chat group members can call GetGroupWithId
to retrieve the chat group attributes from memory, including the chat group ID, name, description, owner, and admin list.
All chat group members can also call GetGroupSpecificationFromServer
to retrieve the chat group attributes from the server, including the chat group ID, name, description, owner, admin list, and member list.
The following code sample shows how to retrieve the chat group attributes:
Retrieve the chat group member list
All chat group members can call GetGroupMemberListFromServer
to retrieve the chat group member list from the server.
The following code sample shows how to retrieve the chat group member list:
Retrieve the chat group list
Users can call FetchJoinedGroupsFromServer
to retrieve the joined chat group list from the server, as shown in the following code sample:
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. The code sample is as follows:
Users can also call FetchPublicGroupsFromServer
to retrieve public chat group list from the server with pagination, as shown in the following code sample:
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:
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:
Check whether a user blocks a chat group
All chat group members can call GetGroupSpecificationFromServer
to check whether they block a chat group according to the Group#MessageBlocked
field.
The following code sample shows how to check whether a user blocks a chat group:
Listen for chat group events
To monitor the chat group events, users can listen for the callbacks in the IGroupManagerDelegate
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: