Skip to main content

Join Chatroom

There are several scenarios for joining a chatroom:

  • (Recommended) The application server uses RC's server-side API Create Chatroom to generate a chatroom ID and distributes it to clients. After obtaining the chatroom ID, clients join via the joinExistChatRoom: method.
  • (Deprecated) Applications directly call IMLib SDK's chatroom joining method to create and join the chatroom. This approach is no longer recommended, and related APIs were deprecated in version 5.6.3 and may be removed in the future.
  • IMLib SDK automatically rejoins unexited chatrooms after network reconnection, requiring no application-level handling.

The application server can register server callbacks for [Chatroom Status Update Callback] to receive notifications about successful chatroom creation and member join events. Clients can receive related notifications through IMLib SDK's [Chatroom Event Monitoring].

Limitations

  • By default, a user cannot join multiple chatrooms simultaneously. Joining a new chatroom automatically exits the previous one. You can enable Single User Joins Multiple Chatrooms in the Service Configuration page of RC Console's IM service.
  • The client-side chatroom joining method allows fetching recent historical messages upon entry (default 10, maximum 50) but doesn't support specifying message types. Enable Join Chatroom with Specified Message Configuration in the Service Configuration page to restrict message types fetched when joining.

Join Existing Chatroom

tip

Starting from IMLib SDK 5.6.3, new methods support returning RCJoinChatRoomResponse in success callbacks, containing chatroom creation time, member count, mute status, and user mute status.

For IMLib SDK versions ≥ 5.6.3, use RCChatRoomClient's joinExistChatRoom:messageCount:successBlock:errorBlock: method to join an existing chatroom.

Method Signature

- (void)joinExistChatRoom:(NSString *)targetId
messageCount:(int)messageCount
successBlock:(nullable void (^)(RCJoinChatRoomResponse *response))successBlock
errorBlock:(nullable void (^)(RCErrorCode status))errorBlock;

Parameters

ParameterTypeDescription
targetIdNSStringChatroom conversation ID (max 64 chars).
messageCountintNumber of historical messages to fetch (1-50). -1 fetches none. 0 uses SDK default (10 messages).
successBlockBlockSuccess callback. response contains RCJoinChatRoomResponse with chatroom metadata and current user status.
errorBlockBlockFailure callback. status returns error code RCErrorCode.

Example Code

[[RCChatRoomClient sharedChatRoomClient] joinExistChatRoom:@"your_chat_room_id"
messageCount:-1
successBlock:^(RCJoinChatRoomResponse *response) {
NSLog(@"Successfully joined the chat room");
// Creation time (millisecond timestamp)
long long createTime = response.createTime;
// Member count
NSInteger memberCount = response.memberCount;
// Global mute status
BOOL isAllChatRoomBanned = response.isAllChatRoomBanned;
// Current user mute status
BOOL isCurrentUserBanned = response.isCurrentUserBanned;
// Current user's chatroom-specific mute status
BOOL isCurrentChatRoomBanned = response.isCurrentChatRoomBanned;
// Current user's allowlist status
BOOL isCurrentChatRoomInWhitelist = response.isCurrentChatRoomInWhitelist;
// Join time (supported from v5.10.2)
BOOL joinTime = response.joinTime;
}
errorBlock:^(RCErrorCode status) {
NSLog(@"Failed to join the chat room. Error code: %ld", (long)status);
}];

For IMLib SDK versions < 5.6.3, use RCChatRoomClient's deprecated joinExistChatRoom:messageCount:success:error: method (marked obsolete in 5.6.3).

Method Signature

- (void)joinExistChatRoom:(NSString *)targetId
messageCount:(int)messageCount
success:(nullable void (^)(void))successBlock
error:(nullable void (^)(RCErrorCode status))errorBlock;

Parameters

ParameterTypeDescription
targetIdNSStringChatroom conversation ID (max 64 chars).
messageCountintNumber of historical messages to fetch (1-50). -1 fetches none. 0 uses SDK default (10 messages).
successBlockBlockSuccess callback.
errorBlockBlockFailure callback. status returns error code RCErrorCode.

Example Code

[[RCChatRoomClient sharedChatRoomClient] joinExistChatRoom:@"chatroomId" messageCount:20 success:^{

} error:^(RCErrorCode status) {

}];

Join Chatroom

tip

This method was deprecated in IMLib SDK 5.6.3.

The joinChatRoom interface creates and joins a chatroom. If the chatroom exists, it simply joins. When using server callbacks for [Chatroom Status Update Callback], RC sends chatroom creation notifications to your specified server address.

Method Signature

- (void)joinChatRoom:(NSString *)targetId
messageCount:(int)messageCount
success:(nullable void (^)(void))successBlock
error:(nullable void (^)(RCErrorCode status))errorBlock

Parameters

ParameterTypeDescription
targetIdNSStringChatroom ID (max 64 chars).
messageCountintNumber of historical messages to fetch (1-50). -1 fetches none. 0 uses SDK default (10 messages).
successBlockBlockSuccess callback.
errorBlockBlockFailure callback. status returns error code RCErrorCode.

Example Code

[[RCChatRoomClient sharedChatRoomClient] joinChatRoom:@"chatroomId" messageCount:20 success:^{

} error:^(RCErrorCode status) {

}];

Rejoining 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 any app-side handling. The app can receive notifications by [monitoring chatroom status].

tip

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

  • For SDK versions < 5.3.1, the SDK will not fetch any messages.
  • For SDK versions ≧ 5.3.1, the SDK will fetch a fixed number of messages based on the messageCount parameter specified when joining the chatroom. The fetched messages may already exist locally, so the app may need to deduplicate them before display.