Manage chat room members
Updated
Shows how to use the Agora Chat SDK to manage the members of a chat room in your app.
Chat rooms enable real-time messaging among multiple users.
This page shows how to use the Chat SDK to manage the members of a chat room in your app.
Understand the tech
The Chat SDK provides theRoom, IRoomManager, and IRoomManagerDelegate classes for chat room management, which allow 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 limit of the Chat APIs supported by different pricing plans as described in Limitations.
- You understand the number of chat rooms supported by different pricing plans as described in Pricing Plan Details.
Implementation
This section introduces how to call the APIs provided by the Chat SDK to implement the features listed above.
Remove a member from a chat room
Only the chat room owner and admins can call DeleteRoomMembers to remove one or more members from a chat room. Once removed from the chat room, this member receives the OnRemovedFromRoom callback, while all the other members receive the OnMemberExitedFromRoom callback. Users can join the chat room again after being removed.
The following code sample shows how to remove a member from a chat room:
SDKClient.Instance.RoomManager.DeleteRoomMembers(roomId, members, new CallBack(
onSuccess: () => {
},
onError: (code, desc) => {
}
));Retrieve the chat room member list
All chat room members can call FetchRoomMembers to retrieve the member list of the current chat room.
The following code sample shows how to retrieve the chat room member list:
// pageSize: The number of chat room members to retrieve per page. The maximum value is 1000.
// cursor: The starting position for data query. Pass in `null` or an empty string at the first call and the server returns chat room members, starting from the latest one.
SDKClient.Instance.RoomManager.FetchRoomMembers(roomId, cursor, pageSize, callback: new ValueCallBack>(
// `members` is of the CursorResult type
onSuccess: (members) => {
},
onError: (code, desc) => {
}
));Manage the chat room blocklist
Add a member to the chat room blocklist
Only the chat room owner and admins can call BlockRoomMembers to add the specified member to the chat room blocklist. Once added to the blocklist, this member receives the OnRemovedFromRoom callback, while all the other members receive the OnMemberExitedFromRoom callback. After being added to blocklist, this user cannot send or receive messages in the chat room. They can no longer join the chat room again until they are removed from the blocklist.
The following code sample shows how to add a member to the chat room blocklist:
SDKClient.Instance.RoomManager.BlockRoomMembers(roomId, members, new CallBack(
onSuccess: () =>
{
},
onError: (code, desc) =>
{
}
));Remove a member from the chat room blocklist
Only the chat room owner and admins can call UnBlockRoomMembers to remove the specified member from the chat room blocklist.
The following code sample shows how to remove a member from the chat room blocklist:
SDKClient.Instance.RoomManager.UnBlockRoomMembers(roomId, members, new CallBack(
onSuccess: () => {
},
onError: (code, desc) => {
}
));Retrieve the chat room blocklist
Only the chat room owner and admins can call FetchRoomBlockList to retrieve the chat room blocklist.
The following code sample shows how to retrieve the chat room blocklist:
SDKClient.Instance.RoomManager.FetchRoomBlockList(roomId, pageNum, pageSize, callback: new ValueCallBack>(
// `list` is of List type
onSuccess: (list) => {
},
onError: (code, desc) => {
}
));Manage the chat room allow list
The chat room owner and admins are added to the chat room allow list by default.
Messages sent by members in the chat room allow list are of high priority and will be delivered first, but there is no guarantee that they will be successfully delivered. When the load is high, the server discards low-priority messages first. If the load is high even then, the server also discards high-priority messages.
Add a member to the chat room allow list
Only the chat room owner and admins can call AddAllowListMembers to add the specified member to the chat room allow list. Members in the chat room allow list can send chat room messages even when the chat room owner or admin has muted all chat room members. However, if a member is already in the chat room mute list, this member still cannot send messages in the chat room even after being added to the chat room allow list. Once added to the allow list, this member and all the other chat room admins or owner receive the IRoomManagerDelegate#OnAddAllowListMembersFromChatroom callback.
The following code sample shows how to add a member to the chat room allow list:
SDKClient.Instance.RoomManager.AddAllowListMembers(roomId, list, new CallBack(
onSuccess: () => {
},
onError: (code, desc) => {
}
));Remove a member from the chat room allow list
Only the chat room owner and admins can call RemoveAllowListMembers to remove the specified member from the chat room allow list. Once removed from the chat room allow list, this member and all the other chat room admins or owner receive the IRoomManagerDelegate#OnRemoveAllowListMembersFromChatroom callback.
The following code sample shows how to remove a member from the chat room allow list:
SDKClient.Instance.RoomManager.RemoveAllowListMembers(roomId, list, new CallBack(
onSuccess: () => {
},
onError: (code, desc) => {
}
));Check whether a user is added to the allow list
All chat room members can call CheckIfInRoomAllowList to check whether they are added to the chat room allow list.
The following code sample shows how to check whether a user is on the chat room allow list:
SDKClient.Instance.RoomManager.CheckIfInRoomAllowList(roomId, new ValueCallBack(
onSuccess: (b) => {
},
onError: (code, desc) => {
}
));Manage the chat room mute list
Add a member to the chat room mute list
Only the chat room owner and admins can call MuteRoomMembers to add the specified member to the chat room mute list. Once added to the mute list, this member and all the other chat room admins or owner receive the OnMuteListAddedFromRoom callback.
Note: The chat room owner can mute chat room admins and regular members, whereas chat room admins can only mute regular members.
The following code sample shows how to add a member to the chat room mute list:
// muteMilliseconds: The mute duration. If you pass `-1`, members are muted permanently.
SDKClient.Instance.RoomManager.MuteRoomMembers(roomId, members, new CallBack(
onSuccess: () => {
},
onError: (code, desc) => {
}
));Remove a member from the chat room mute list
Only the chat room owner and admins can call UnMuteRoomMembers to remove the specified member from the chat room mute list. Once removed from the mute list, this member and all the other chat room admins or owner receive the OnMuteListRemovedFromRoom callback.
Note: The chat room owner can unmute chat room admins and regular members, whereas chat room admins can only unmute regular members.
The following code sample shows how to remove a member from the chat room mute list:
SDKClient.Instance.RoomManager.UnMuteRoomMembers(roomId, members, new CallBack(
onSuccess: () => {
},
onError: (code, desc) => {
}
));Retrieve the chat room mute dictionary
Only the chat room owner and admins can call FetchRoomMuteList to retrieve the chat room mute dictionary.
The following code sample shows how to retrieve the chat room mute dictionary:
SDKClient.Instance.RoomManager.FetchRoomMuteList(roomId, pageSize, pageNum, callback: new ValueCallBack>(
onSuccess: (dict) => {
},
onError: (code, desc) => {
}
));Mute and unmute all the chat room members
Mute all the chat room members
Only the chat room owner and admins can call MuteRoomMembers to mute all the chat room members. Once all the members are muted, the IRoomManagerDelegate#OnAllMemberMuteChangedFromChatroom callback is triggered and only those in the chat room allow list can send messages in the chat room.
Unlike muting a chat room member, this kind of mute does not expire automatically after a certain period and you need to call the UnMuteAllRoomMembers method to unmute all members in the chat room.
The following sample code shows how to mute all the chat room members:
SDKClient.Instance.RoomManager.MuteRoomMembers(roomId, members, new CallBack(
onSuccess: () => {
},
onError: (code, desc) => {
}
));Unmute all the chat room members
Only the chat room owner and admins can call UnMuteAllRoomMembers to unmute all the chat room members. All members in the chat room can receive the IRoomManagerDelegate#OnAllMemberMuteChangedFromChatroom event and only those in the chat room allow list can send messages in the chat room.
The following sample code shows how to unmute all the chat room members:
SDKClient.Instance.RoomManager.UnMuteAllRoomMembers(roomId, new ValueCallBack(
onSuccess: (room) => {
},
onError: (code, desc) => {
}
));Manage the chat room owner and admins
Transfer the chat room ownership
Only the chat room owner can call ChangeRoomOwner to transfer the ownership to the specified chat room member. Once the ownership is transferred, the former chat room owner becomes a regular member. The new chat room owner and the chat room admins receive the OnOwnerChangedFromRoom callback.
The following code sample shows how to transfer the chat room ownership:
SDKClient.Instance.RoomManager.ChangeRoomOwner(roomId, newOwner, new CallBack(
onSuccess: () => {
},
onError: (code, desc) => {
}
));Add a chat room admin
Only the chat room owner can call AddRoomAdmin to add an admin. Once promoted to an admin, the new admin and the other chat room admins receive the OnAdminAddedFromRoom callback.
The following code sample shows how to add a chat room admin:
SDKClient.Instance.RoomManager.AddRoomAdmin(roomId, adminId, new CallBack(
onSuccess: () => {
},
onError: (code, desc) => {
}
));Remove a chat room admin
Only the chat room owner can call RemoveRoomAdmin to remove an admin. Once demoted to a regular member, the former admin and the other chat room admins receive the OnAdminRemovedFromRoom callback.
The following code sample shows how to remove a chat room admin:
SDKClient.Instance.RoomManager.RemoveRoomAdmin(roomId, adminId, new CallBack(
onSuccess: () => {
},
onError: (code, desc) => {
}
));Listen for chat Room Events
For details, see Chat Room Events.
