Skip to main content

Manage chat room members

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 ChatManager and ChatRoom classes for chat room management, which allows you to implement the following features:

  • Remove a member from a chat room
  • Retrieve the member list of a chat room
  • Manage the block list of a chat room
  • Manage the mute list of a chat room
  • Manage the chat room allow list
  • Mute and unmute all the chat room members
  • Manage the chat room allow list
  • Manage the owner and admins of a chat room
  • Mute and unmute all the chat room members

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.

Manage chat room members

All the chat room members can call fetchChatRoomMembers to retrieve the member list of the current chat room.


_1
Map<String, Long> members = ChatClient.getInstance().chatroomManager().fetchChatRoomMembers(chatRoomId, cursor, pageSize);

The chat room owner and admin can call removeChatRoomMembers to remove the specified member from the chat room. Once a member is removed, this member receives the onRemovedFromChatRoom callback, and the other chat room members receive the onMemberExited callback. After being removed from a chat room, you can join this chat room again.


_1
ChatClient.getInstance().chatroomManager().removeChatRoomMembers(chatRoomId, members);

Manage the chat room block list

The chat room owner and admin can add the specified member into the chat room block list and remove them from it. Once a chat room member is added to the block list, this member cannot send or receive chat room messages, nor can this member join the chat room again.


_18
// The chat room owner or admin call blockChatroomMembers to add the specified member to the chat room block list.
_18
ChatRoom chatRoom = ChatClient.getInstance().chatroomManager().blockChatroomMembers(chatRoomId, members);
_18
_18
// The chat room owner or admin call unblockChatroomMembers to remove the specified user out of the block list.
_18
ChatRoom chatRoom = ChatClient.getInstance().chatroomManager().unblockChatRoomMembers(chatRoomId, members);
_18
_18
// The chat room owner or admin call fetchChatRoomBlackList to reveive the block list of the current chat room.
_18
ChatClient.getInstance().chatroomManager().fetchChatRoomBlackList(chatRoomId, new ValueCallBack<List<String>>() {
_18
@Override
_18
public void onSuccess(List<String> value) {
_18
_18
}
_18
_18
@Override
_18
public void onError(int error, String errorMsg) {
_18
_18
}
_18
});

Manage the chat room mute list

To manage the messages in the chat room, the chat room owner and admin can add the specified member to the chat room mute list and remove them from it. Once a chat room member is added to the mute list, this member can no longer send chat room message, not even after being added to the chat room allow list.


_8
// The chat room owner or admin call muteChatRoomMembers to add the specified user to the chat room block list. The muted member and all the other chat room admins or owner receive the onMuteListAdded callback.
_8
ChatRoom chatRoom = ChatClient.getInstance().chatroomManager().muteChatRoomMembers(chatRoomId, members, duration);
_8
_8
// The chat room owner or admin can call unMuteChatRoomMembers to remove the specified user from the chat room block list. The unmuted member and all the other chat room admins or owner receive the onMuteListRemoved callback.
_8
ChatRoom chatRoom = ChatClient.getInstance().chatroomManager().unMuteChatRoomMembers(chatRoomId, members);
_8
_8
// The chat room owner or admin can call fetchChatRoomMuteList to fetch the mute list of the current chat room.
_8
Map<String, Long> memberMap = ChatClient.getInstance().chatroomManager().fetchChatRoomMuteList(chatRoomId, pageNum, pageSize);

Mute and unmute all the chat room members

The chat room owner or admin can mute or unmute all the chat room members using muteAllMembers. Once all the members are muted, only those in the chat room allow list can send messages in the chat room.


_25
// The chat room owner or admin can call muteAllMembers to mute all the chat room members. Once all the members are muted, these members receive the onAllMemberMuteStateChanged callback.
_25
ChatClient.getInstance().chatroomManager().muteAllMembers(chatRoomId, new ValueCallBack<ChatRoom>() {
_25
@Override
_25
public void onSuccess(ChatRoom value) {
_25
_25
}
_25
_25
@Override
_25
public void onError(int error, String errorMsg) {
_25
_25
}
_25
});
_25
_25
// The chat room owner or admin can call unmuteAllMembers to unmute all the chat room members. Once all the members are unmuted, these members receive the onAllMemberMuteStateChanged callback.
_25
ChatClient.getInstance().chatroomManager().unmuteAllMembers(chatRoomId, new ValueCallBack<ChatRoom>() {
_25
@Override
_25
public void onSuccess(ChatRoom value) {
_25
_25
}
_25
_25
@Override
_25
public void onError(int error, String errorMsg) {
_25
_25
}
_25
});

Manage 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 the chat room members using muteAllMembers. However, if a member is already in the chat room mute list, adding this member to the allow list does not take effect.

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 delivered. When the load is high, the server discards low-priority messages first. If the load is still high even then, the server discards high-priority messages.


_51
// The chat room owner or admin can call addToChatRoomWhiteList to add the specified member to the chat room allow list.
_51
ChatClient.getInstance().chatroomManager().addToChatRoomWhiteList(chatRoomId, members, new ValueCallBack<ChatRoom>() {
_51
@Override
_51
public void onSuccess(ChatRoom value) {
_51
_51
}
_51
_51
@Override
_51
public void onError(int error, String errorMsg) {
_51
_51
}
_51
});
_51
_51
// The chat room owner or admin can call removeFromChatRoomWhiteList to remove the specified member from the chat room allow list.
_51
ChatClient.getInstance().chatroomManager().removeFromChatRoomWhiteList(chatRoomId, members, new ValueCallBack<ChatRoom>() {
_51
@Override
_51
public void onSuccess(ChatRoom value) {
_51
_51
}
_51
_51
@Override
_51
public void onError(int error, String errorMsg) {
_51
_51
}
_51
});
_51
_51
// Chat room members can call checkIfInChatRoomWhiteList to check whether they are in the chat room allow list.
_51
ChatClient.getInstance().chatroomManager().checkIfInChatRoomWhiteList(chatRoomId, new ValueCallBack<Boolean>() {
_51
@Override
_51
public void onSuccess(Boolean value) {
_51
_51
}
_51
_51
@Override
_51
public void onError(int error, String errorMsg) {
_51
_51
}
_51
});
_51
_51
// The chat room owner or admin can call fetchChatRoomWhiteList to retrieve the allow list of the current chat room.
_51
ChatClient.getInstance().chatroomManager().fetchChatRoomWhiteList(chatRoomId, new ValueCallBack<List<String>>() {
_51
@Override
_51
public void onSuccess(List<String> value) {
_51
_51
}
_51
_51
@Override
_51
public void onError(int error, String errorMsg) {
_51
_51
}
_51
});

Manage chat room ownership and admin

The chat room owner can transfer the ownership to the specified chat room member. Once the ownership is transferred, the original chat room owner becomes a regular member. The new chat room owner and the chat room admins receive the onOwnerChanged callback.

The chat room owner can also add admins. Once added to the chat room admin list, the newly added admin and the other chat room admins receive the onAdminAdded callback.


_8
// The chat room owner can call changeOwner to transfer the ownership to the other chat room member.
_8
ChatRoom chatRoom = ChatClient.getInstance().chatroomManager().changeOwner(chatRoomId, newOwner);
_8
_8
// The chat room owner can call addChatRoomAdmin to add the chat room admin.
_8
ChatRoom chatRoom = ChatClient.getInstance().chatroomManager().addChatRoomAdmin(chatRoomId, admin);
_8
_8
// The chat room owner can call removeChatRoomAdmin to remove the chat room admin. The removed admin and the other admins receive the onAdminRemoved callback.
_8
ChatRoom chatRoom = ChatClient.getInstance().chatroomManager().removeChatRoomAdmin(chatRoomId, admin);

Listen for chat room events

For details, see Chat Room Events.

vundefined