Skip to main content

Reconnection Mechanism and Reconnection Kick Policy

Automatic Reconnection Mechanism

The SDK has a built-in automatic reconnection mechanism. Once a connection is successfully established, the SDK's reconnection mechanism immediately takes effect and handles all reconnection processes. When the connection is lost due to network issues, the SDK will automatically attempt to reconnect without requiring any additional connection operations from you.

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

  • Poor network conditions: The SDK may continuously attempt to reconnect. Since there is a keep-alive mechanism between the client SDK and RC's server, if the network is too weak and causes a heartbeat timeout, 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 attempt to reconnect.
  • App running in the background: When the app remains in the background for more than 2 minutes or is suspended by the system, the IMLib SDK will actively disconnect. The IMLib SDK will trigger a reconnection attempt when the app returns to the foreground.
tip

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

Reconnection Interval

When the SDK attempts to reconnect, the interval between attempts gradually increases: 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 interval resets to the initial sequence, ensuring the fastest possible reconnection under these conditions.

Manually Exiting the Reconnection Mechanism

If 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 other devices when it successfully reconnects.

By default, the IM service only allows a single user Token to be logged in on one mobile device at a time. If a subsequent mobile device successfully logs in, it will automatically kick out the previously logged-in device. In some cases, the SDK's reconnection mechanism may prevent the newly logged-in device from staying online.

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 due to unstable network conditions, the connection fails, triggering the SDK's automatic reconnection mechanism.
  2. The user then attempts to log in on mobile device B. Device B successfully connects, and the user can use the IM service normally on B.
  3. When device A's network stabilizes, the SDK successfully reconnects. Since device A is now the last device to come online, device B is kicked out.

Modifying the Reconnection Kick Policy for App Users

If app users want device A to be logged out upon reconnection while keeping device B logged in, they can modify the reconnection kick policy for the current user (User ID).

tip

Before using this API, you must enable the Allow Client SDK to Control Reconnection Kick Policy feature in the RC Console under Chat > Chat settings > Multi client.

Configure whether to kick out the reconnecting device upon reconnection. This method must be called before init.

Parameter Description

ParameterTypeDescription
enablebooleanWhether to kick out the currently reconnecting device. See Detailed Explanation of enable Parameter below.

Detailed Explanation of enable Parameter:

  • Set setReconnectKickEnable to YES

    If another mobile device is already online during reconnection, the reconnection will be aborted, leaving the already logged-in device unaffected.

  • Set setReconnectKickEnable to NO

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

Example Code

[[RCCoreClient sharedCoreClient] setReconnectKickEnable:YES];