Skip to main content

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.

tip

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:

  1. 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.
  2. 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.
  3. 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).

tip

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

ParameterTypeDescription
enableBooleanWhether to kick out reconnecting devices. See Detailed Explanation of enable Parameter below.

Sample Code

RongIMClient.getInstance().setReconnectKickEnable(enable);

Detailed Explanation of enable Parameter:

  • Set enable to true:

    If another mobile device is already online during reconnection, the current device will not reconnect, leaving the already logged-in device unaffected.

  • Set enable to false:

    If another mobile device is already online during reconnection, the online device will be kicked out, allowing the current device to log in.