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 the ChatRoom, ChatRoomManager, and ChatRoomEventListener classes for chat room 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 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 removeChatRoomMembers to remove one or more members from a chat room. Once removed from the chat room, this member receives the onRemoved callback, while all the other members receive the onMemberExited callback. Users can join the chat room again after being removed.
The following code sample shows how to remove members from a chat room:
List members = new List();
members.Add("member1");
members.Add("member2");
SDKClient.Instance.RoomManager.DeleteRoomMembers(roomId, members, new CallBack(
onSuccess: () => {
Console.WriteLine($"DeleteRoomMembers success.");
},
onError: (code, desc) => {
Console.WriteLine($"DeleteRoomMembers failed, code:{code}, desc:{desc}");
}
));Retrieve the chat room member list
All chat room members can call fetchChatRoomMembers 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.
ChatClient.getInstance()
.roomManager.fetchChatRoomMembers(roomId, cursor, pageSize)
.then((members) => {
console.log("get members success.", members);
})
.catch((reason) => {
console.log("get members fail.", reason);
});Manage the chat room blocklist
Add a member to the chat room blocklist
Only the chat room owner and admins can call blockChatRoomMembers to add the specified member to the chat room blocklist. Once added to the blocklist, this member receives the onRemoved callback, while all the other members receive the onMemberExited 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:
ChatClient.getInstance()
.roomManager.blockChatRoomMembers(roomId, members)
.then(() => {
console.log("block members success.");
})
.catch((reason) => {
console.log("block members fail.", reason);
});Remove a member from the chat room blocklist
Only the chat room owner and admins can call unblockChatRoomMembers 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:
ChatClient.getInstance()
.roomManager.unblockChatRoomMembers(roomId, members)
.then(() => {
console.log("unblock members success.");
})
.catch((reason) => {
console.log("unblock members fail.", reason);
});Retrieve the chat room blocklist
Only the chat room owner and admins can call fetchChatRoomBlockList to retrieve the chat room blocklist.
The following code sample shows how to retrieve the chat room blocklist:
ChatClient.getInstance()
.roomManager.fetchChatRoomBlockList(roomId, pageNum, pageSize)
.then((members) => {
console.log("block members success.", members);
})
.catch((reason) => {
console.log("block members fail.", reason);
});Manage the chat room mute list
Add a member to the chat room mute list
Only the chat room owner and admins can call muteChatRoomMembers 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 onMuteListAdded 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:
// duration: The mute duration. If you pass `-1`, members are muted permanently.
ChatClient.getInstance()
.roomManager.muteChatRoomMembers(roomId, muteMembers, duration)
.then(() => {
console.log("mute members success.");
})
.catch((reason) => {
console.log("mute members fail.", reason);
});Remove a member from the chat room mute list
Only the chat room owner and admins can call unMuteChatRoomMembers 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 onMuteListRemoved 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:
ChatClient.getInstance()
.roomManager.unMuteChatRoomMembers(roomId, unMuteMembers)
.then(() => {
console.log("unMute members success.");
})
.catch((reason) => {
console.log("unMute members fail.", reason);
});Retrieve the chat room mute list
Only the chat room owner and admins can call fetchChatRoomMuteList to retrieve the chat room mute list.
The following code sample shows how to retrieve the chat room mute list:
ChatClient.getInstance()
.roomManager.fetchChatRoomMuteList(roomId, pageNum, pageSize)
.then((members) => {
console.log("get mute members success.", members);
})
.catch((reason) => {
console.log("get mute members fail.", reason);
});Check if a member is on the chat room mute list
All chat room members can call isMemberInChatRoomMuteList to check whether they are on the mute list of a specific chat room. When called, the method returns a boolean value indicating the member’s current mute list status.
The following code sample shows how to check whether you are on the chat room mute list:
ChatClient.getInstance()
.roomManager.isMemberInChatRoomMuteList('roomId123')
.then((v) => {
console.log('Whether I am in the chat room mute list:', v);
})
.catch((error) => {
console.log('Failed to check if I am in the chat room mute list:', error);
});Manage the chat room allow list
The chat room owner and admins are added to the chat room allow list by default.
Members in the chat room allow list can send chat room messages even when the chat room owner or an admin has muted all the 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.
Retrieve the allow list of the chat room
Only the chat room owner or admin can call fetchChatRoomAllowListFromServer to retrieve the allow list of the current chat room.
ChatClient.getInstance()
.roomManager.fetchChatRoomAllowListFromServer(roomId)
.then((members) => {
console.log("get members success.", members);
})
.catch((reason) => {
console.log("get members fail.", reason);
});Add a member to the chat room allow list
Only the chat room owner or admin can call addMembersToChatRoomAllowList to add a member to the chat room allow list. The member added to the allow list, the chat room owner, and admins, except the operator, receive the ChatRoomEventListener#onAllowListAdded event.
ChatClient.getInstance()
.roomManager.addMembersToChatRoomAllowList(roomId, members)
.then((members) => {
console.log("success.", members);
})
.catch((reason) => {
console.log("fail.", reason);
});Remove a member from the chat room allow list
Only the chat room owner or admin can call removeMembersFromChatRoomAllowList to remove a member from the chat room allow list. The member removed from the allow list, the chat room owner, and admins, except the operator, receive the ChatRoomEventListener#onAllowListRemoved event.
ChatClient.getInstance()
.roomManager.removeMembersFromChatRoomAllowList(roomId, members)
.then((members) => {
console.log("success.", members);
})
.catch((reason) => {
console.log("fail.", reason);
});Check whether the current user is in the chat room allow list
Chat room members can call isMemberInChatRoomAllowList to check whether they are in the chat room allow list.
ChatClient.getInstance()
.roomManager.isMemberInChatRoomAllowList(roomId)
.then((members) => {
console.log("success.", members);
})
.catch((reason) => {
console.log("fail.", reason);
});Mute and unmute all the chat room members
Mute all the chat room members
Only the chat room owner and admins can call muteAllChatRoomMembers to mute all the chat room members. Once all the members are muted, the ChatRoomEventListener#onAllChatRoomMemberMuteStateChanged 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 unMuteAllChatRoomMembers method to unmute all members in the chat room.
The following sample code shows how to mute all the chat room members:
ChatClient.getInstance()
.roomManager.muteAllChatRoomMembers(roomId)
.then((members) => {
console.log("success.", members);
})
.catch((reason) => {
console.log("fail.", reason);
});Unmute all the chat room members
Only the chat room owner and admins can call unMuteAllChatRoomMembers to unmute all the chat room members. Once all the members are muted, the onAllChatRoomMemberMuteStateChanged callback is triggered.
The following sample code shows how to unmute all the chat room members:
try {
await ChatClient.getInstance.chatRoomManager.unMuteAllChatRoomMembers();
} on ChatError catch (e) {
}Manage the chat room owner and admins
Transfer the chat room ownership
Only the chat room owner can call changeOwner 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 onOwnerChanged callback.
The following code sample shows how to transfer the chat room ownership:
ChatClient.getInstance()
.roomManager.changeOwner(roomId, newOwner)
.then(() => {
console.log("change owner success.");
})
.catch((reason) => {
console.log("change owner fail.", reason);
});Add a chat room admin
Only the chat room owner can call addChatRoomAdmin to add an admin. Once promoted to an admin, the new admin and the other chat room admins receive the onAdminAdded callback.
The following code sample shows how to add a chat room admin:
ChatClient.getInstance()
.roomManager.addChatRoomAdmin(roomId, admin)
.then(() => {
console.log("add admin success.");
})
.catch((reason) => {
console.log("add admin fail.", reason);
});Remove a chat room admin
Only the chat room owner can call removeChatRoomAdmin to remove an admin. Once demoted to a regular member, the former admin and the other chat room admins receive the onAdminRemoved callback.
The following code sample shows how to remove a chat room admin:
ChatClient.getInstance()
.roomManager.removeChatRoomAdmin(roomId, admin)
.then(() => {
console.log("remove admin success.");
})
.catch((reason) => {
console.log("remove admin fail.", reason);
});Listen for chat room events
For details, see Chat Room Events.
