Reconnection Mechanism and Reconnection Kick
Automatic Reconnection Mechanism
The IMLib SDK has an built-in 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 automatically attempt to re-establish the connection without requiring any additional manual operations.
Possible scenarios that may trigger the SDK's reconnection mechanism include:
-
Weak network conditions: The SDK may continuously attempt to reconnect. Since there is a keep-alive mechanism between the client SDK and RC's server, once the heartbeat times out due to poor network quality, the SDK will trigger reconnection attempts until successful.
-
No network connection: The SDK's reconnection mechanism will pause. Once the network is restored, the SDK will resume reconnection attempts.
-
App running in background: When the app is in the background, the system may restrict network access or process execution, leading to connection drops. If network access is restricted, the SDK will keep retrying until successful; if process execution is restricted, the SDK will trigger reconnection when the app returns to the foreground.
Once a connection error callback is triggered, the SDK will exit the reconnection mechanism. Handle this scenario based on specific status codes in your business logic.
Reconnection Interval
The SDK uses progressively increasing intervals for reconnection attempts: 0.05s (0s before version 5.6.2), 0.25s, 0.5s, 1s, 2s, 4s, 8s, 16s, and 32s. After that, it retries every 64s.
When the app returns to the foreground or network conditions change, the reconnection interval resets to the initial values to ensure rapid reconnection.
Manually Exiting Reconnection Mechanism
When the app actively disconnects, the SDK will exit the reconnection mechanism and stop attempting to reconnect.
Reconnection Kick Policy
The reconnection kick policy controls whether the SDK should log out devices when automatically reconnecting successfully.
By default, the IM service only allows a single user Token to be logged in on one mobile device at a time. When a subsequent mobile device successfully connects, it will automatically kick out the previously logged-in device. In some cases, the SDK's reconnection mechanism may prevent subsequent devices from staying online normally.
For example, the default reconnection kick policy may lead to the following scenario:
- A user attempts to log in on mobile device A, but fails to connect due to unstable network conditions, 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 IM services normally on B.
- When device A's network stabilizes, the SDK reconnects successfully. Since device A is now the subsequently connected device, it kicks out device B.
Modifying Reconnection Kick Policy for App Users
If app users want device A to be logged out upon successful reconnection while keeping device B logged in for the above scenario, they can modify the reconnection kick policy for the current user (User ID).
Before using this interface, you must enable Allow client SDK to adjust reconnection mutual kick strategy in the RC Console.
Configure whether to kick out reconnecting devices when offline. This method must be called before init.
Parameter Description
| Parameter | Type | Description |
|---|---|---|
enable | Boolean | Whether to kick out reconnecting devices. See Detailed Explanation of enable Parameter below. |
Sample Code
RongIMClient.getInstance().setReconnectKickEnable(enable);
Detailed Explanation of enable Parameter:
-
Set
enabletotrue:If another mobile device is already online during reconnection, the current device will not reconnect, leaving the already logged-in device unaffected.
-
Set
enabletofalse:If another mobile device is already online during reconnection, the online device will be kicked out, allowing the current device to log in.