Skip to main content

Join a Chatroom

After the application server creates a chatroom, it can distribute the chatroom ID to clients. Clients 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 Chatroom.

  • The application server can receive event notifications such as successful chatroom creation and member join 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, see [Chatroom Service Configuration].

  • The IMLib SDK can automatically rejoin chatrooms after reconnecting from a network outage.

Join an Existing Chatroom

tip
  1. The Electron platform has provided this interface since version 5.7.0.
  2. Starting from version 5.8.3, the success callback's data returns chatroom information and mute status. For details, see [IChatroomJoinResponse].

If the chatroom already exists, you can call [joinExistChatRoom] to join it after obtaining the chatRoomId. This method can only join existing chatrooms.

Interface

RongIMLib.joinExistChatRoom(chatRoomId, options)


#### Parameter Description

| Parameter | Type | Required | Description |
|:--- |:--- |:--- |:--- |
| chatRoomId | string | Yes | The chatroom ID. The application server should call the IM Server API to create the chatroom. For details, see [Create Chatroom](/platform-chat-api/chatroom/create). |
| options | object | Yes | Options |


- **options Description**:

| Parameter | Type | Required | Description |
|:--- |:--- |:--- |:--- |
| count | number | Yes | The number of historical messages to fetch when joining 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). |
| extra | string | No | Additional information. Default is empty. |


#### Example Code

```js
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)
})


## Join Chatroom \{#joinChatRoom}

:::tip
IMLib deprecated this method starting from version 5.8.3
:::

The [joinChatRoom] interface creates and joins a chatroom. If the chatroom already exists, it joins directly. If you use the server callback [Chatroom Status Update Callback], Rong Cloud will send a notification of successful chatroom creation to your specified server address.


#### Interface

```js
RongIMLib.joinChatRoom(chatRoomId, options)


#### Parameter Description

| Parameter | Type | Required | Description |
|:--- |:--- |:--- |:--- |
| chatRoomId | string | Yes | The chatroom ID. The application server should call the IM Server API to create the chatroom. For details, see [Create Chatroom](/platform-chat-api/chatroom/create). |
| options | object | Yes | Options |


- **options Description**:

| Parameter | Type | Required | Description |
|:--- |:--- |:--- |:--- |
| count | number | Yes | The number of historical messages to fetch when joining 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). |
| extra | string | No | Additional information. Default is empty. |


#### Example Code

```js
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)
})


## Rejoin Chatroom After Reconnection \{#rejoin}

The SDK has a 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 App intervention. The App can receive notifications through the chatroom status callback.

:::tip
- In scenarios 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 may already exist locally, so the App may need to deduplicate them before display.

- Starting from version 5.7.0, Electron supports the behavior of fetching messages after the SDK rejoins the chatroom during network reconnection. SDK versions below 5.7.0 will not fetch messages.
:::


<!-- typedoc -->
[joinChatRoom]: https://doc.rongcloud.cn/apidoc/im-web/latest/zh_CN/modules.html#joinChatRoom
[joinExistChatRoom]: https://doc.rongcloud.cn/apidoc/im-web/latest/zh_CN/modules.html#joinExistChatRoom
[IChatroomJoinResponse]: https://doc.rongcloud.cn/apidoc/im-web/latest/zh_CN/interfaces/IChatroomJoinResponse.html
[Chatroom Service Configuration]: ./service-config
[Chatroom Status Update Callback]: /platform-chat-api/chatroom/status