Skip to main content

Reconnection mechanism and reconnection kick strategy

Automatic reconnection mechanism

The Chat SDK implements an automatic reconnection mechanism. Once connected, this mechanism takes effect immediately and handles all reconnection processes. When disconnection occurs due to network issues, the SDK automatically attempts to reestablish the connection without requiring additional manual operations.

Common scenarios that trigger reconnection:

  • Poor network conditions: May cause continuous reconnection attempts. The SDK maintains keep-alive heartbeats with the server - if network quality degrades enough to cause heartbeat timeouts, the SDK initiates reconnection attempts until successful.
  • No network connection: The reconnection mechanism pauses. When network connectivity returns, the SDK resumes reconnection attempts.
  • App in background: When the app enters background or gets suspended by the system, the SDK triggers reconnection upon returning to foreground.
tip

When connection error callbacks are triggered, the SDK exits the reconnection mechanism. Handle these cases manually based on the specific status codes.

Reconnection interval

The SDK uses an incremental backoff strategy for reconnection intervals. The current reconnection process is managed internally - when the app returns to foreground or network status changes, the interval timer resets to ensure faster reconnection in these scenarios.

Manually exiting reconnection

When the app actively disconnects, the SDK exits the reconnection mechanism and stops attempting to reconnect.

Reconnection kick strategy

The reconnection kick strategy determines whether the SDK should log out other devices when automatically reconnecting successfully.

By default, the instant messaging service only allows one mobile device per user account to stay logged in. When a new mobile device connects successfully, it automatically kicks out previously logged-in devices. In some cases, the SDK's reconnection mechanism may prevent newly connected devices from staying online.

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

  1. User Zhangsan attempts to log in on mobile device A, but fails due to unstable network, triggering the SDK's automatic reconnection.
  2. The user then logs in successfully on mobile device B and can use the messaging service normally.
  3. When device A's network stabilizes and reconnects successfully, it kicks out device B since A was the last device to connect.

Modifying the reconnection kick strategy

If you want to keep device B logged in while forcing device A to disconnect after reconnection, you can modify the reconnection kick strategy.

Chat UI provides the reconnectKickEnable parameter in NCConnectParams for this purpose. Configure it before establishing the connection:

Objective C
NCConnectParams *params = [[NCConnectParams alloc] initWithToken:@"access token from server"];
params.reconnectKickEnable = YES;