Skip to main content

Reconnection and device conflict

Automatic reconnection

The SDK has a built-in automatic reconnection mechanism. Once a connection is established, the SDK handles all reconnection logic. When the connection drops due to network issues, the SDK automatically attempts to reconnect without any action required from your app.

Scenarios that may trigger reconnection:

  • Weak network: The SDK may continuously reconnect. A heartbeat timeout caused by poor connectivity triggers reconnection attempts until the connection succeeds.
  • No network: Reconnection pauses. Once the network recovers, the SDK reconnects.
  • App in background: After approximately 2 minutes in the background (or when the system suspends the app), the SDK disconnects. It reconnects automatically when the app returns to the foreground.
tip

Once a connection error callback fires, the SDK exits the reconnection loop. Handle the error based on the specific status code.

Reconnection intervals

The SDK uses exponential backoff for reconnection attempts: 0.05s, 0.25s, 0.5s, 1s, 2s, 4s, 8s, 16s, 32s, then every 64s.

When the app switches to the foreground or the network state changes, the interval resets to ensure a fast reconnection.

Stop reconnection manually

Calling disconnect explicitly stops the reconnection mechanism.

Reconnection kick strategy

The reconnection kick strategy controls whether a reconnecting device should displace an already-online device.

By default, only one mobile device can be online per user at a time. When a second mobile device connects, it kicks the first device offline. In some cases, the SDK's automatic reconnection may cause the later-connected device to go offline unexpectedly.

Example scenario:

  1. User tries to log in on device A, but a network issue prevents connection. SDK starts auto-reconnecting on device A.
  2. User switches to device B and connects successfully.
  3. Device A's network stabilizes and the SDK reconnects. Since A is now the "later" device, it kicks device B offline.

Configure the reconnection kick strategy

To keep device B online and disconnect device A in the above scenario, configure the reconnectKickEnable property on ConnectParams (NCConnectParams in Objective-C).

tip

Before using this feature, enable Allow client SDK to control reconnection kick strategy in the Nexconn Console under Chat > Chat settings > Multi Client.

ParameterTypeDescription
reconnectKickEnableBOOLWhether to kick the reconnecting device. Defaults to NO.

Parameter details:

  • Set reconnectKickEnable = true: If another mobile device is already online when this device reconnects, this device stops reconnecting. The already-online device is not affected.
  • Set reconnectKickEnable = false (default): If another mobile device is already online when this device reconnects, the already-online device is kicked offline and this device comes online.
swift
import NexconnChatSDK

let params = ConnectParams(token: "your_token")
params.reconnectKickEnable = true
NCEngine.connect(
params: params,
databaseOpenedHandler: nil,
completionHandler: { userId, error in
if let error {
print("Connect failed: \(error.localizedDescription)")
return
}
print("Connected as: \(userId ?? "")")
}
)