Join a Chatroom
Joining a chatroom can be done in the following ways:
- (Recommended) The application backend creates a chatroom via the IM server API Create a 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's method to join a chatroom, creating and joining the chatroom. This method is no longer recommended, and the related API has been deprecated in version 5.6.3 and may be removed in the future.
- The SDK will automatically rejoin the chatroom after a network reconnection, and no additional handling is required by the application.
The application backend can register a server callback Chatroom Status Update Callback in advance to receive notifications about successful chatroom creation and member join events. 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 Specify Message Configuration When Joining a Chatroom on the RC Console's Basic features page to restrict the types of messages fetched when joining a chatroom.
Joining an Existing Chatroom
Starting from version 5.6.3, IMLib has added a new method that supports returning RCJoinChatRoomResponse in the success callback, which includes the chatroom's creation time, member count, chatroom mute status, user mute status, and more.
If the IMLib version is ≧ 5.6.3, you can use the RCChatRoomClient's joinExistChatRoom:messageCount:successBlock:errorBlock: method to join an existing chatroom.
[[RCChatRoomClient sharedChatRoomClient] joinExistChatRoom:@"your_chat_room_id"
messageCount:-1
successBlock:^(RCJoinChatRoomResponse *response) {
NSLog(@"Successfully joined the chatroom");
// Creation time (millisecond timestamp)
long long createTime = response.createTime;
// Member count
NSInteger memberCount = response.memberCount;
// Whether the chatroom is globally muted
BOOL isAllChatRoomBanned = response.isAllChatRoomBanned;
// Whether the current user is muted
BOOL isCurrentUserBanned = response.isCurrentUserBanned;
// Whether the current user is muted in this chatroom
BOOL isCurrentChatRoomBanned = response.isCurrentChatRoomBanned;
// Whether the current user is in the chatroom's allowlist
BOOL isCurrentChatRoomInWhitelist = response.isCurrentChatRoomInWhitelist;
// Time of joining the chatroom (supported from version 5.10.2)
BOOL joinTime = response.joinTime;
}
errorBlock:^(RCErrorCode status) {
NSLog(@"Failed to join the chatroom. Error code: %ld", (long)status);
}];
Parameter | Type | Description |
---|---|---|
targetId | NSString | The chatroom conversation ID, with a maximum length of 64 characters. |
messageCount | int | The number of historical messages to fetch when 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). |
successBlock | Block | Callback for successfully joining the chatroom. response returns an RCJoinChatRoomResponse object, which includes basic chatroom information and the current user's status in the chatroom. |
errorBlock | Block | Callback for failing to join the chatroom. status returns the error code RCErrorCode. |
If the IMLib version is < 5.6.3, you can use the RCChatRoomClient
's joinExistChatRoom:messageCount:success:error: method to join an existing chatroom. This method has been deprecated in version 5.6.3.
[[RCIMClient sharedRCIMClient] joinExistChatRoom:@"chatroomId" messageCount:20 success:^{
} error:^(RCErrorCode status) {
}];
Parameter | Type | Description |
---|---|---|
targetId | NSString | The chatroom conversation ID, with a maximum length of 64 characters. |
messageCount | int | The number of historical messages to fetch when 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). |
successBlock | Block | Callback for successfully joining the chatroom |
errorBlock | Block | Callback for failing to join the chatroom. status returns the error code RCErrorCode. |
Joining a Chatroom
The joinChatRoom
method has been deprecated since version 5.6.3.
The joinChatRoom
interface creates and joins a chatroom. If the chatroom already exists, it simply 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.
[[RCIMClient sharedRCIMClient] joinChatRoom:@"chatroomId" messageCount:20 success:^{
} error:^(RCErrorCode status) {
}];
Parameter | Type | Description |
---|---|---|
targetId | NSString | The chatroom ID, with a maximum length of 64 characters. |
messageCount | int | The number of historical messages to fetch when 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). |
successBlock | Block | Callback for successfully joining the chatroom |
errorBlock | Block | Callback for failing to join the chatroom. status returns the error code RCErrorCode. |
Rejoining a 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 had not exited, the SDK will automatically rejoin the chatroom without requiring the App to handle it. The App can receive notifications by Listening to Chatroom Status.
In the case of a 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 any messages.
- If the SDK version is ≧ 5.3.1, the SDK will fetch a fixed number of messages based on the
messageCount
passed when joining the chatroom. The fetched messages may already exist locally, and the App may need to deduplicate them before displaying.