Chat rooms enable real-time messaging among multiple users.
This page shows how to use the Agora Chat SDK to manage the members of a chat room in your app.
The Agora Chat SDK provides the ChatRoom
, ChatRoomManager
, and ChatRoomEventListener
classes for chat room management, which allows you to implement the following features:
Before proceeding, ensure that you meet the following requirements:
This section describes how to call the APIs provided by the Agora Chat SDK to implement chat room features.
Only the chat room owner and admins can call removeChatRoomMembers
to remove the specified member 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 a member from a chat room:
ChatClient.getInstance()
.roomManager.removeChatRoomMembers(roomId, members)
.then(() => {
console.log("remove members success.");
})
.catch((reason) => {
console.log("remove members fail.", reason);
});
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:
ChatClient.getInstance()
.roomManager.fetchChatRoomMembers(roomId, cursor, pageSize)
.then((members) => {
console.log("get members success.", members);
})
.catch((reason) => {
console.log("get members fail.", reason);
});
Only the chat room owner and admins can call blockChatRoomMembers
to add the specified member to the chat room block list. Once added to the block list, this member receives the onRemoved
callback, while all the other members receive the onMemberExited
callback. After being added to block list, 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 block list.
The following code sample shows how to add a member to the chat room block list:
ChatClient.getInstance()
.roomManager.blockChatRoomMembers(roomId, members)
.then(() => {
console.log("block members success.");
})
.catch((reason) => {
console.log("block members fail.", reason);
});
Only the chat room owner and admins can call unblockChatRoomMembers
to remove the specified member from the chat room block list.
The following code sample shows how to remove a member from the chat room block list:
ChatClient.getInstance()
.roomManager.unblockChatRoomMembers(roomId, members)
.then(() => {
console.log("unblock members success.");
})
.catch((reason) => {
console.log("unblock members fail.", reason);
});
Only the chat room owner and admins can call fetchChatRoomBlockList
to retrieve the chat room block list.
The following code sample shows how to retrieve the chat room block list:
ChatClient.getInstance()
.roomManager.fetchChatRoomBlockList(roomId, pageNum, pageSize)
.then((members) => {
console.log("block members success.", members);
})
.catch((reason) => {
console.log("block members fail.", reason);
});
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:
ChatClient.getInstance()
.roomManager.muteChatRoomMembers(roomId, muteMembers, duration)
.then(() => {
console.log("mute members success.");
})
.catch((reason) => {
console.log("mute members fail.", reason);
});
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);
});
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);
});
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);
});
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);
});
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);
});
For details, see Chat Room Events.