Skip to main content

Join Chatroom

Joining a chatroom can be categorized into the following scenarios:

  • (Recommended) The application backend creates a chatroom via the IM service API Create Chatroom and distributes the chatroom ID to the client. The client can join the chatroom after obtaining the chatroom ID.
  • (Deprecated) The application directly calls the client SDK method to join a chatroom, creating and joining the chatroom. This method is no longer recommended, and the related API was deprecated in version 5.6.3 and may be removed in the future.
  • The SDK will automatically rejoin the chatroom after reconnecting from a network disconnection, and no application handling is required.

The application backend can register a server callback Chatroom Status Update Callback in advance to receive notifications for events such as successful chatroom creation and member joining. The client can receive related notifications by Listening to Chatroom Events.

Limitations

  • By default, a 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 RC Console's Basic Features page.
  • The client's method for joining a chatroom allows fetching the latest historical messages upon joining (default is 10, maximum is 50), but does not support specifying message types. You can enable Join Chatroom with Specified Message Configuration on the RC Console's Basic Features page to restrict the types of messages fetched 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.

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 joining
System.out.println("Successfully joined the 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 the chatroom is globally muted
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 the allowlist of this chatroom
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 joining errors
System.out.println("Failed to join the chatroom. Error code: " + e.getValue());
// Provide appropriate handling and user feedback based on the error code
}
});

The successful callback returns JoinChatRoomResponse, which includes information such as the chatroom's creation time, member count, chatroom mute status, and user mute status.

ParameterTypeDescription
chatRoomIdStringChatroom session ID, maximum length is 64 characters.
defMessageCountintNumber of historical messages fetched upon joining the chatroom, range: 1-50. If -1 is passed, no historical messages are fetched. If 0 is passed, the SDK default setting is used (default is 10).
callbackResultCallback<JoinChatRoomResponse>Callback interface.

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.

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) {

}
});

Join Chatroom

tip

This method was deprecated starting from IMLib version 5.6.3.

The joinChatRoom interface creates and joins a chatroom. If the chatroom already exists, it directly joins. If you have registered the server callback Chatroom Status Update Callback, RC will send a notification of successful chatroom creation 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) {

}
});
ParameterTypeDescription
chatRoomIdStringChatroom session ID, maximum length is 64 characters.
defMessageCountintNumber of historical messages fetched upon joining the chatroom, range: 1-50. If -1 is passed, no historical messages are fetched. If 0 is passed, the SDK default setting is used (default is 10).
callbackOperationCallbackCallback interface

Rejoin Chatroom After Reconnection

The SDK has a reconnection mechanism. After a successful reconnection, if the currently logged-in user had previously joined a chatroom and has not exited, the SDK will automatically rejoin the chatroom, and no app handling is required. The app can receive notifications by Listening to Chatroom Status.

tip

In the scenario of network 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, and the app may need to deduplicate them before displaying.