Skip to main content

Reconnection Mechanism and Reconnection Kick Strategy

Automatic Reconnection Mechanism

The SDK has implemented an automatic reconnection mechanism. Once the connection is successfully established, the SDK's reconnection mechanism will immediately take effect and handle all reconnection processes. When the connection is lost due to network issues, the SDK will internally attempt to re-establish the connection, and no additional connection operations are required from you.

Possible scenarios that may trigger the SDK's reconnection mechanism include:

Since there is a keep-alive mechanism between the client SDK and the RC server, if the network is too poor or there is no network, causing a heartbeat timeout, the SDK will trigger a reconnection operation and keep trying to reconnect until the connection is successful.

Note

Once a connection error callback is triggered, the SDK will exit the reconnection mechanism. Please handle it according to the specific status code.

Reconnection Interval

The SDK attempts to reconnect at an interval of 5 seconds.

Manually Exiting the Reconnection Mechanism

When the application actively disconnects, the SDK will exit the reconnection mechanism and will no longer attempt to reconnect.

Reconnection Kick Strategy (Electron Only)

Note

The user-level reconnection kick strategy is only available in the Electron Solution. Web applications do not support setting the reconnection kick strategy.

The reconnection kick strategy is used to control whether the SDK should kick out the previously logged-in device when it successfully reconnects.

By default, the instant messaging service only allows a single user account to log in on one desktop device at a time. If a user logs in on a new desktop device, the previously logged-in device will be automatically kicked out. In some cases, the SDK's reconnection mechanism may prevent the newly logged-in device from staying online.

For example, the default reconnection kick strategy may lead to the following scenario:

  1. User Zhangsan attempts to log in on desktop device A, but due to unstable network conditions, the connection fails, triggering the SDK's automatic reconnection mechanism.
  2. The user then tries to log in on desktop device B. Device B successfully connects, and the user can use the instant messaging service normally on device B.
  3. When the network on device A stabilizes, the SDK successfully reconnects. Since device A is the last device to connect, device B is kicked out.

Modifying the Reconnection Kick Strategy for App Users

If the app user wishes to keep device B logged in while kicking out device A after reconnection in the above scenario, they can modify their reconnection kick strategy. The setting to determine whether to kick out the reconnecting device upon successful reconnection needs to be configured during the connection establishment process.

tip

Before using this interface, you must enable Allow SDK to Modify Reconnection Kick Strategy in the RC Console under Configuration > Chat settings > Basic features > Others.

Example Call

RongIMLib.connect('<Your-Token>', true).then(res => {
if (res.code === RongIMLib.ErrorCode.SUCCESS) {
console.log('Connection successful, connected user ID: ', res.data.userId);
} else {
console.warn('Connection failed, code:', res.code)
}
})

Parameter Description

ParameterTypeDescription
reconnectKickEnablebooleanWhether to kick out the reconnecting device. See the detailed explanation below for the reconnectKickEnable parameter.

Detailed Explanation of the reconnectKickEnable Parameter:

  • Set reconnectKickEnable to true:

    If another desktop device is already online during reconnection, the SDK will not reconnect, ensuring that the currently logged-in device remains unaffected.

  • Set reconnectKickEnable to false:

    If another desktop device is already online during reconnection, the SDK will kick out the currently logged-in device and allow the reconnecting device to go online.