Reconnection and device conflict
Automatic reconnection
The SDK has a built-in automatic reconnection mechanism. Once a connection is established, the SDK manages all reconnection handling. When a disconnection occurs due to network issues, the SDK automatically attempts to reconnect without any additional action on your part.
Scenarios that may trigger automatic reconnection:
- Weak network: The SDK may reconnect continuously. A keep-alive mechanism between the client SDK and server triggers reconnection when heartbeat timeouts occur due to poor network conditions. The SDK retries until it connects successfully.
- No network: The reconnection mechanism pauses. Once the network recovers, the SDK resumes reconnection.
When a connection error callback fires, the SDK exits the reconnection mechanism. Handle the specific status code accordingly.
Reconnection intervals
The SDK uses progressively increasing reconnection intervals: 0.05s, 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 interval resets to the beginning to ensure a fast reconnection.
Exit the reconnection mechanism
When the app actively disconnects, the SDK exits the reconnection mechanism and stops retrying.
Reconnection kick strategy
The reconnection kick strategy controls whether devices should be taken offline when the SDK reconnects successfully.
By default, only one mobile device per user account can be online at a time. When a second mobile device connects, it automatically kicks the previously connected device. In some cases, the SDK's reconnection mechanism may prevent the most recently logged-in device from staying online.
For example, the default strategy may cause the following:
- User Alice tries to log in on mobile device A, but an unstable network prevents a successful connection, triggering automatic reconnection.
- Alice switches to mobile device B and logs in successfully.
- Device A's network stabilizes, and the SDK reconnects. Because A is now the most recently connected device, it kicks device B offline.
Modify the reconnection kick strategy
If you want to keep device B online and kick the reconnecting device A offline in the scenario above, modify the reconnection kick strategy for the current user.
Before using this feature, enable Allow Client SDK to Control Reconnection Kick Strategy in the Nexconn console under Chat > Chat settings > Multi Client.
Set whether to kick the reconnecting device on reconnection. This is an engine-level configuration that must be set before initialization.
await NCEngine.initialize(InitParams(
appKey: '<your-app-key>',
kickReconnectDevice: true,
));