Skip to main content

Monitor Chatroom Events

The chatroom service provides three types of event listeners for client apps: chatroom status listener, chatroom member change listener, and chatroom event notification listener.

Through the listeners provided by the SDK, client apps can obtain information from the RC server about the destruction status of chatrooms, the status of users joining or leaving chatrooms on the current and other clients, notifications of members entering or exiting chatrooms, and information related to muting or banning members in chatrooms.

ListenerNameDescription
[ChatRoomAdvancedActionListener]Chatroom Status ListenerReceives events of users joining or leaving chatrooms and information about the destruction status of chatrooms on the current client.
[ChatRoomMemberActionListener]Chatroom Member Change ListenerReceives information about other users joining or leaving the chatroom where the current user is located.
ChatRoomNotifyEventListenerChatroom Event Notification ListenerReceives information related to muting or banning members in the current chatroom; receives information about the current user joining or leaving the chatroom on other devices.

Monitor Chatroom Status

Applications can monitor events of users joining or leaving chatrooms and changes in the destruction status of chatrooms on the current client.

Use the addChatRoomAdvanceActionListener or setChatRoomAdvancedActionListener methods of RongChatRoomClient to add or set the [ChatRoomAdvancedActionListener] listener.

// addChatRoomAdvanceActionListener, since 5.2.5
RongChatRoomClient.addChatRoomAdvanceActionListener(
new ChatRoomAdvancedActionListener() {
@Override
public void onJoining(String chatRoomId) {
// default implementation ignored
}

@Override
public void onJoined(String chatRoomId) {
// Chatroom joined successfully, deprecated
// default implementation ignored
}

@Override
public void onJoined(String chatRoomId, JoinChatRoomResponse joinChatRoomResponse) {
// Chatroom joined successfully since 5.6.3
// default implementation ignored
}

@Override
public void onReset(String chatRoomId) {
// default implementation ignored
}

@Override
public void onQuited(String chatRoomId) {
// default implementation ignored
}

@Override
public void onDestroyed(String chatRoomId, ChatRoomDestroyType type) {
data.setFirstAndNotify(Pair.create(chatRoomId, type));
}

@Override
public void onError(String chatRoomId, CoreErrorCode code) {
// default implementation ignored
}
});


// setChatRoomAdvanceActionListener
RongChatRoomClient.setChatRoomAdvanceActionListener(listener);

Remove the listener:

// removeChatRoomAdvanceActionListener, since 5.2.5
RongChatRoomClient.addChatRoomAdvanceActionListener(listener);
// setChatRoomAdvanceActionListener
RongChatRoomClient.setChatRoomAdvanceActionListener(null);

Chatroom Status Events

The following table lists the events and callback methods provided by the chatroom operation listener ChatRoomAdvancedActionListener.

Callback MethodTrigger TimingDescription
onJoining(String chatRoomId)The current user is joining the chatroomReturns data including: chatroom ID
onJoined(String chatRoomId, JoinChatRoomResponse)The current user has joined the chatroom (available since 5.6.3).Returns data including: chatroom ID, chatroom room information, and the user's status in the chatroom.
onJoined(String chatRoomId)The current user has joined the chatroom (deprecated since 5.6.3)Returns data including: chatroom ID
onReset(String chatRoomId)The chatroom where the user is located has been reset.Returns data including: chatroom ID. Note: After the chatroom is reset, the onJoining and onJoined callbacks will be triggered immediately.
onQuited(String chatRoomId)The current user has left the chatroomReturns data including: chatroom ID
onDestroyed(String chatRoomId, IRongCoreEnum.ChatRoomDestroyType type)The current user is online, and the chatroom has been destroyedReturns data including: chatroom ID, chatroom destruction type. The chatroom destruction type is ChatRoomDestroyType.MANUAL, indicating that the chatroom was destroyed by calling the IM Server API. ChatRoomDestroyType.AUTO indicates that the chatroom was automatically destroyed.
onError(String chatRoomId, IRongCoreEnum.CoreErrorCode code)An error occurred during chatroom operationReturns data including: chatroom ID, error code.

Monitor Chatroom Member Changes

tip

The SDK has supported monitoring chatroom member changes since version 5.1.4.

Since version 5.1.4, the SDK has provided [ChatRoomMemberActionListener] in RongChatRoomClient, allowing the currently logged-in user to monitor the joining and leaving behaviors of other members in the chatroom.

Enable the Service

You can enable this feature in the RC Console under Configuration > Chat settings > Basic features > Chatroom > Chatroom Member Change Monitoring. After enabling, the system will send callback notifications to other members in the chatroom when users join or leave, which will increase the message volume.

Set the Chatroom Member Change Listener

Set the chatroom member change listener [ChatRoomMemberActionListener]. Since version 5.6.7, a new callback method returning the ChatRoomMemberActionModel object has been added, which includes the current number of members in the chatroom.

RongChatRoomClient.setChatRoomMemberListener(new RongChatRoomClient.ChatRoomMemberActionListener() {
/**
* @param chatRoomMemberActions
* @param roomId
*/
@Override
public void onMemberChange(List<ChatRoomMemberAction> chatRoomMemberActions, String chatRoomId) {
// Handle the member change with the list of ChatRoomMemberAction objects and the chatRoomId
}

/**
* @since 5.6.7
*/
@Override
public void onMemberChange(ChatRoomMemberActionModel model) {
// Handle the member change with the ChatRoomMemberActionModel object
String chatroomId = model.getRoomId();
List<ChatRoomMemberAction> chatRoomMemberActions = model.getChatRoomMemberActions();
int memberCount = model.getMemberCount();

for (ChatRoomMemberAction action : chatRoomMemberActions) {
// Access the properties of ChatRoomMemberAction object
String userId = action.getUserId();
ChatRoomMemberAction.ChatRoomMemberActionType actionType = action.getChatRoomMemberAction();

if (actionType == ChatRoomMemberAction.ChatRoomMemberActionType.CHAT_ROOM_MEMBER_JOIN) {
System.out.println("Action Type: Join");
} else if (actionType == ChatRoomMemberAction.ChatRoomMemberActionType.CHAT_ROOM_MEMBER_QUIT) {
System.out.println("Action Type: Leave");
}
}
}
});

When other users join or leave the chatroom, the SDK will trigger both onMemberChange callback methods, both of which are called back on the UI thread. The [ChatRoomMemberAction] list contains information about the members who have joined or left the chatroom. The ChatRoomMemberActionModel additionally encapsulates the current number of members in the chatroom (referring to the number of members who have joined but not left). It is recommended to use the onMemberChange method that returns the ChatRoomMemberActionModel object.

If the current user is disconnected from the chatroom due to network reasons, they will not be able to monitor the joining and leaving behaviors of other members during the disconnection period.

Monitor Chatroom Event Notifications

tip

The SDK has supported monitoring chatroom event notifications since version 5.4.5.

Applications can monitor notifications related to muting or banning members in the current chatroom, as well as notifications related to the current user joining or leaving the chatroom on other devices.

Use the addChatRoomNotifyEventListener method of RongChatRoomClient to add a ChatRoomNotifyEventListener listener. The SDK has supported the RCChatRoomNotifyEventListener listener since version 5.4.5.

// Since 5.4.5
RongChatRoomClient.addChatRoomNotifyEventListener(
new RongChatRoomClient.ChatRoomNotifyEventListener() {
@Override
public void onChatRoomNotifyMultiLoginSync(ChatRoomSyncEvent event) {

}

@Override
public void onChatRoomNotifyBlock(ChatRoomMemberBlockEvent event) {

}

@Override
public void onChatRoomNotifyBan(ChatRoomMemberBanEvent event) {
}
});

// Remove
RongChatRoomClient.removeChatRoomNotifyEventListener(listener);
Callback MethodTrigger TimingDescription
onChatRoomNotifyMultiLoginSync(ChatRoomSyncEvent event)When the user is logged in on multiple devices and joins or leaves the chatroom on another clientReceives events of the user joining or leaving the chatroom on the current client
onChatRoomNotifyBlock(ChatRoomMemberBlockEvent event)When a muting or unmuting event occurs in the chatroom where the user is locatedWhether to receive notifications depends on whether needNotify is set to true when calling the relevant server API for muting or unmuting.
onChatRoomNotifyBan(ChatRoomMemberBanEvent event)When a user is banned or unbanned in the chatroom where the user is locatedWhether to receive notifications depends on whether needNotify is set to true when calling the relevant server API for banning or unbanning.

Multi-Device Login Event Notifications

When the user is logged in on multiple devices and joins or leaves the chatroom on another client, the onChatRoomNotifyMultiLoginSync(ChatRoomSyncEvent event) method is triggered.

Trigger ScenarioNotification ScopeDescription
The user is logged in on multiple devices and joins the chatroom on one deviceThe user who joinedWhen the current user joins the chatroom on one device, other online devices will receive notifications, while offline devices will not. Returns data including: chatroom ID, join time.
The user is logged in on multiple devices and leaves the chatroom on one deviceThe user who leftWhen the current user leaves the chatroom on one device, other online devices will receive notifications, while offline devices will not. Returns data including: chatroom ID, leave time.
The user is logged in on multiple devices and is already in a chatroom, and joins a new chatroom causing them to be kicked out of the previous chatroomThe current user and all members of the kicked-out chatroomIf Single User Joins Multiple Chatrooms is enabled in the console, notifications will not be sent. Returns data including: chatroom ID, kick-out time.

The specific attributes of ChatRoomSyncEvent are defined as follows:

AttributeTypeDescription
chatroomIdStringChatroom ID.
statusChatRoomSyncStatusThe changed status, Quit(0): Leave. Join(1): Join.
reasonChatRoomSyncStatusReasonIf status is Quit, distinguish the leave type: LeaveOnMyOwn(1) means the user left on their own. OtherDeviceLogin(2) means the user was kicked out due to joining on another device. If status is Join, the value of reason can be ignored.
timelongThe timestamp when the user joined/left/was kicked out, accurate to milliseconds.
extraStringAdditional information.

When a ban or unban event occurs in the chatroom where the user is located, and the relevant Server API is called with needNotify set to true, the onChatRoomNotifyBlock(ChatRoomMemberBlockEvent event) method is triggered.

Trigger ScenarioNotification ScopeDescription
[Ban Chatroom User][doc-page-封禁聊天室用户]All members in the chatroomReturns data including: chatroom ID, list of banned user IDs, ban time and duration, additional information.
[Unban Chatroom User][doc-page-解除封禁聊天室用户]The unbanned memberReturns data including: chatroom ID, current user ID, additional information.

The specific attributes of ChatRoomMemberBlockEvent are defined as follows:

AttributeTypeDescription
chatroomIdStringChatroom ID.
operateTypeChatRoomOperateTypeBan type. Deblock(0): Unban. Blocked(1): Ban.
durationTimelongBan duration, in milliseconds, with a maximum value of 43200 minutes (1 month) and a minimum value of 1 minute. This field is valid when operateType is Blocked.
operateTimelongOperation timestamp (milliseconds).
userIdListList<String>List of banned user IDs. When unbanning, this is the current user ID.
extraStringAdditional information.

When a mute or unmute event occurs in the chatroom where the user is located, and the relevant Server API is called with needNotify set to true, the onChatRoomNotifyBan(ChatRoomMemberBanEvent event) method is triggered.

Trigger ScenarioNotification ScopeDescription
[Mute Specified Chatroom User][doc-page-禁言指定聊天室用户]All members in the chatroomReturns data including: chatroom ID, list of muted user IDs, mute time and duration, additional information.
[Unmute Specified Chatroom User][doc-page-取消禁言指定聊天室用户]All members in the chatroomReturns data including: chatroom ID, list of unmuted user IDs, unmute time, additional information.
[Set Chatroom-Wide Mute][doc-page-设置聊天室全体禁言]All members in the chatroomReturns data including: chatroom ID, mute time, additional information.
[Cancel Chatroom-Wide Mute][doc-page-取消聊天室全体禁言]All members in the chatroomReturns data including: chatroom ID, unmute time, additional information.
[Add to Chatroom-Wide Mute Allowlist][doc-page-加入聊天室全体禁言白名单]All members in the chatroomReturns data including: chatroom ID, list of user IDs added to the allowlist, allowlist setting time, additional information.
[Remove from Chatroom-Wide Mute Allowlist][doc-page-移出聊天室全体禁言白名单]All members in the chatroomReturns data including: chatroom ID, list of user IDs removed from the allowlist, allowlist removal time, additional information.
[Global Mute User][doc-page-全局禁言用户]The globally muted userReturns data including: chatroom ID, mute time and duration, additional information.
[Cancel Global Mute User][doc-page-取消全局禁言用户]The globally unmuted userReturns data including: chatroom ID, unmute time, additional information.

The specific attributes of ChatRoomMemberBanEvent are defined as follows:

AttributeTypeDescription
chatroomIdStringChatroom ID.
banTypeChatRoomMemberBanTypeChatroom mute/unmute operation type enumeration
durationTimelongMute duration, in milliseconds: maximum value is 43200 minutes (1 month), minimum value is 1 minute. This field is valid when banType is a mute type.
operateTimelongOperation timestamp (milliseconds).
userIdListList<String>List of muted user IDs. When unmuting, this is the current user ID.
extraStringAdditional information.