Join a Chatroom
After the application server creates a chatroom, it can distribute the chatroom ID to the client. The client can join the chatroom after obtaining the chatroom ID.
- The application server can call the IM Server API to create a chatroom. For details, refer to the server documentation Create a Chatroom.
- The application server can receive notifications about successful chatroom creation and member join events through Chatroom Status Update Callback.
- By default, the same user cannot join multiple chatrooms simultaneously. When an app user joins a new chatroom, they will automatically exit the previous one. You can enable Single User Joins Multiple Chatrooms in the Console. For details, refer to Chatroom Service Configuration.
- The IMLib SDK can automatically rejoin the chatroom after reconnecting from a network disconnection.
Join an Existing Chatroom
- The Electron platform has provided this interface since version 5.7.0.
- Starting from version 5.8.3, the success callback's data returns chatroom information and mute status. For details, refer to IChatroomJoinResponse.
If the chatroom already exists, you can call joinExistChatRoom to join the chatroom after obtaining the chatRoomId
. This method can only join existing chatrooms.
const chatRoomId = "Chatroom ID";
const count = 50;
RongIMLib.joinExistChatRoom(chatRoomId, {
count: count
}).then(res => {
// Successfully joined the chatroom
const { code, data } = res;
if(code === RongIMLib.ErrorCode.SUCCESS){
console.log(code, data)
} else {
console.log(code)
}
}).catch(error => {
console.log(error)
})
Parameter | Type | Description |
---|---|---|
chatRoomId | string | The chatroom ID. The application server should call the IM Server API to create the chatroom. For details, refer to Create a Chatroom. |
count | number | The 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). |
Join a Chatroom
The joinChatRoom method has been deprecated in IMLib since version 5.8.3.
The joinChatRoom interface creates and joins a chatroom. If the chatroom already exists, it directly joins. If you use the server callback Chatroom Status Update Callback, RC will send a notification of successful chatroom creation to your specified server address.
const chatRoomId = "Chatroom ID";
const count = 50;
RongIMLib.joinChatRoom(chatRoomId, {
count: count
}).then(res => {
// Successfully joined the chatroom
if (res.code === RongIMLib.ErrorCode.SUCCESS){
console.log(res.code)
} else {
console.log(res.code, res.msg)
}
}).catch(error => {
console.log(error)
})
Parameter | Type | Description |
---|---|---|
chatRoomId | string | The chatroom ID |
count | number | The 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). |
Rejoin 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 did not exit, the SDK will automatically rejoin the chatroom without requiring app intervention. The app can receive notifications through the chatroom status callback.
In the case of network reconnection, after the SDK successfully rejoins the chatroom, it will fetch a fixed number of messages based on the count
value passed when joining the chatroom. The fetched messages might already exist locally, and the app may need to deduplicate them before displaying.
- Starting from version 5.7.0, Electron supports the behavior of fetching messages after the SDK rejoins the chatroom in the case of network reconnection. SDK versions below 5.7.0 will not fetch messages.