Reconnection Mechanism and Reconnection Kick
Automatic Reconnection Mechanism
The SDK has an automatic reconnection mechanism built-in. Once the connection is successful, 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 without requiring any additional connection operations from you.
Abnormal situations that may cause the SDK to disconnect and reconnect include:
- Weak Network Environment: The SDK may continuously attempt to reconnect. Since there is a connection keep-alive mechanism between the client SDK and the RC server, if the network is too poor and causes a heartbeat timeout, the SDK will trigger a reconnection operation and keep trying until the connection is successful.
- No Network Environment: The SDK's reconnection mechanism will pause. Once the network is restored, the SDK will perform a reconnection operation.
Once a connection error callback is triggered, the SDK will exit the reconnection mechanism. Please handle it according to the specific status code.
Reconnection Time Intervals
When the SDK attempts to reconnect, the time intervals increase progressively: 0.05s (0s before version 5.6.2), 0.25s, 0.5s, 1s, 2s, 4s, 8s, 16s, 32s. After that, it retries every 64s.
When the app switches to the foreground or the network status changes, the reconnection time will restart from the above intervals to ensure a quick connection in such cases.
Manually Exiting the Reconnection Mechanism
When the app actively disconnects, the SDK will exit the reconnection mechanism and no longer attempt to reconnect.
Reconnection Kick Strategy
The reconnection kick strategy is used to control whether the device should be kicked offline when the SDK automatically reconnects successfully.
By default, the IM service only allows the same user account to log in on a single mobile device. If a subsequent mobile device connects successfully, the previously logged-in device will be automatically kicked out. In some cases, the SDK's reconnection mechanism may prevent the subsequent device from staying online.
For example, the default reconnection kick strategy may lead to the following scenario:
- User Zhang San attempts to log in on mobile device A, but due to unstable network on device A, the connection fails, triggering the SDK's automatic reconnection mechanism.
- The user then attempts to log in on mobile device B. Device B connects successfully, and the user can use the IM service normally on device B.
- Once the network on device A stabilizes, the SDK reconnects successfully. Since device A is the subsequent device, device B is kicked out.
Modifying the Reconnection Kick Strategy for App Users
If app users wish to keep device B logged in while kicking out device A in the above scenario, they can modify the reconnection kick strategy for the current user (User ID
).
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.
Set whether to kick out the reconnecting device when disconnected. This method must be called before init.
RongIMClient.getInstance().setReconnectKickEnable(enable);
Parameter | Type | Description |
---|---|---|
enable | boolean | Whether to kick out the reconnecting device. See the detailed explanation of the enable parameter below. |
Detailed Explanation of the enable
Parameter:
-
Set
enable
totrue
:If another mobile device is already online during reconnection, the SDK will not reconnect, ensuring the already logged-in device remains unaffected.
-
Set
enable
tofalse
:If another mobile device is already online during reconnection, the SDK will kick out the already online device and bring the current device online.