Skip to main content

Joining a Chatroom

There are several scenarios for joining a chatroom:

  • (Recommended) The application backend creates a chatroom via the IM server API Create Chatroom and distributes the chatroom ID to clients. Clients can join the chatroom after obtaining the chatroom ID.
  • (Deprecated) The application directly calls the client SDK's join chatroom method to create and join the chatroom. This approach is no longer recommended, and the related API was deprecated in version 5.6.3 and may be removed in the future.
  • After an SDK network disconnection and reconnection, it will automatically rejoin the chatroom, requiring no application intervention.

The application backend can register server callbacks in advance for Chatroom Status Update Callback to receive notifications for events such as successful chatroom creation and member joins. Clients can receive related notifications by Monitoring Chatroom Events.

Limitations

  • By default, the same user cannot join multiple chatrooms simultaneously. When a user joins a new chatroom, they will automatically exit the previous one. You can enable Single User Joins Multiple Chatrooms on the Service Configuration page of the RC Console's IM service.
  • The client's join chatroom method allows fetching the latest historical messages upon joining (default 10, maximum 50), but does not support specifying message types. You can enable Join Chatroom with Specified Message Configuration on the Service Configuration page of the RC Console's IM service to restrict fetching only specified message types when joining a chatroom.

Joining an Existing Chatroom

tip

Starting from version 5.6.3, IMLib added an overloaded method that supports returning JoinChatRoomResponse in the callback, which includes information such as the chatroom's creation time, member count, chatroom mute status, and user mute status.

If the IMLib version is ≥ 5.6.3, you can use the joinExistChatRoom method of RongChatRoomClient to join an existing chatroom.

Interface

RongChatRoomClient.getInstance().joinExistChatRoom(chatRoomId, defMessageCount, callback);

Parameter Description

ParameterTypeDescription
chatRoomIdStringChatroom conversation ID, with a maximum length of 64 characters.
defMessageCountIntThe number of historical messages to fetch when entering the chatroom, range: 1-50. If -1 is passed, no historical messages will be fetched. If 0 is passed, the SDK default setting will be used (default is 10 messages).
callbackResultCallback<JoinChatRoomResponse>Callback interface.

Example Code

String chatroomId = "Chatroom ID";
int defMessageCount = 50;

RongChatRoomClient.getInstance().joinExistChatRoom(chatRoomId, defMessageCount, new IRongCoreCallback.ResultCallback<JoinChatRoomResponse>() {
@Override
public void onSuccess(JoinChatRoomResponse response) {
// Handle successful chatroom join
System.out.println("Successfully joined chatroom");

// Parse each return value
long createTime = response.getCreateTime(); // Chatroom creation time (millisecond timestamp)
int memberCount = response.getMemberCount(); // Member count
boolean isAllChatRoomBanned = response.isAllChatRoomBanned(); // Whether global mute is enabled
boolean isCurrentUserBanned = response.isCurrentUserBanned(); // Whether the current user is muted
boolean isCurrentChatRoomBanned = response.isCurrentChatRoomBanned(); // Whether the current user is muted in this chatroom
boolean isCurrentChatRoomInWhitelist = response.isCurrentChatRoomInWhitelist(); // Whether the current user is in this chatroom's allowlist
long joinTime = response.getJoinTime(); // User join time (millisecond timestamp)

// Further process the parsed values as needed
}

@Override
public void onError(IRongCoreEnum.CoreErrorCode e) {
// Handle chatroom join failure
System.out.println("Failed to join chatroom. Error code: " + e.getValue());
// Provide appropriate feedback to the user based on the error code
}
});

If the IMLib version is < 5.6.3, the joinExistChatRoom method's success callback does not carry additional information. This method was deprecated in version 5.6.3.

Example Code

String chatroomId = "Chatroom ID";
int defMessageCount = 50;

RongChatRoomClient.getInstance().joinExistChatRoom(chatroomId, defMessageCount, new IRongCoreCallback.OperationCallback() {

@Override
public void onSuccess() {

}

@Override
public void onError(IRongCoreEnum.CoreErrorCode coreErrorCode) {

}
});

Joining a Chatroom

tip

This method was deprecated in IMLib starting from version 5.6.3.

Interface

RongChatRoomClient.getInstance().joinChatRoom(chatroomId, defMessageCount, callback);

Parameter Description

ParameterTypeDescription
chatRoomIdStringChatroom conversation ID, with a maximum length of 64 characters.
defMessageCountIntThe number of historical messages to fetch when entering the chatroom, range: 1-50. If -1 is passed, no historical messages will be fetched. If 0 is passed, the SDK default setting will be used (default is 10 messages).
callbackOperationCallbackCallback interface

Example Code

The joinChatRoom interface creates and joins a chatroom. If the chatroom already exists, it simply joins. If you have registered server callbacks for Chatroom Status Update Callback, RC will send a chatroom creation success notification to your specified server address.

String chatroomId = "Chatroom ID";
int defMessageCount = 50;

RongChatRoomClient.getInstance().joinChatRoom(chatroomId, defMessageCount, new IRongCoreCallback.OperationCallback() {

@Override
public void onSuccess() {

}

@Override
public void onError(IRongCoreEnum.CoreErrorCode coreErrorCode) {

}
});

Rejoining a Chatroom After Reconnection

The SDK has an automatic reconnection mechanism. After successful reconnection, if the currently logged-in user had previously joined a chatroom and has not exited, the SDK will automatically rejoin the chatroom without requiring application intervention. The application can receive notifications by Monitoring Chatroom Status.

tip

In scenarios involving network disconnection and reconnection, once the SDK successfully rejoins the chatroom, it will automatically fetch a certain number of chatroom messages.

  • If the SDK version is < 5.3.1, the SDK will not fetch messages.
  • If the SDK version is ≥ 5.3.1, the SDK will fetch a fixed number of messages based on the defMessageCount passed when joining the chatroom. The fetched messages may already exist locally, so the application may need to deduplicate them before display.