Reconnection and reconnect kick
Automatic reconnection
The SDK has a built-in automatic reconnection mechanism. Once a connection is established, the mechanism takes effect immediately and handles all reconnection attempts. When the connection drops due to a network issue, the SDK internally attempts to re-establish the connection without requiring any additional action from you.
Common scenarios that trigger a disconnect and reconnection:
- Poor network conditions: The SDK may enter a continuous reconnection loop. The SDK and the Nexconn server maintain a keep-alive mechanism. If a heartbeat times out due to poor network quality, the SDK triggers a reconnection attempt and keeps trying until it succeeds.
- No network: The reconnection mechanism pauses. Once the network recovers, the SDK reconnects.
- App in the background: After the app has been in the background for up to 2 minutes, or when the system suspends the app, the Nexconn Chat SDK proactively disconnects. It triggers a reconnection when the app returns to the foreground.
Once a connection error callback is fired, the SDK exits the reconnection mechanism. Handle the situation based on the specific status code.
Reconnection intervals
The SDK increases the interval between reconnection attempts progressively: 0.05s, 0.25s, 0.5s, 1s, 2s, 4s, 8s, 16s, 32s. After that, it retries every 64s.
When the app returns to the foreground or the network state changes, the reconnection interval resets to the beginning of the sequence, ensuring the fastest possible reconnection in those situations.
Exit the reconnection mechanism
After the application explicitly disconnects, the SDK exits the reconnection mechanism and makes no further reconnection attempts.
Reconnect kick strategy
The reconnect kick strategy controls whether an already-online device is kicked offline when the SDK reconnects successfully.
By default, IM only allows a single user access token to be logged in on one mobile device at a time. When a new mobile device connects successfully, the previously connected device is automatically kicked offline. In some situations, the SDK's automatic reconnection mechanism can cause the most recently logged-in device to be disrupted.
For example, the default reconnect kick strategy can lead to the following scenario:
- A user attempts to log in on mobile device A, but A's unstable network prevents a successful connection, triggering the SDK's automatic reconnection mechanism.
- The user then tries to log in on mobile device B. B connects successfully and the user can use IM normally on B.
- Once A's network stabilizes, the SDK reconnects successfully. Because A is the most recently connected device, B is kicked offline.
Modify the reconnect kick strategy for an app user
If your app user wants device A (the one that just reconnected) to be signed out in the scenario above while keeping device B online, you can modify the reconnect kick strategy for the current user (User ID).
Before using this API, enable Allow Client SDK to Control Reconnect Kick Strategy in the Nexconn Console under Chat > Chat settings > Multi Client.
Configure the reconnect kick strategy before establishing a connection by setting the reconnectKickEnable property on ConnectParams (NCConnectParams in Objective-C).
Parameters
| Parameter | Type | Description |
|---|---|---|
reconnectKickEnable | BOOL | Whether to kick out the device that is currently reconnecting. See Parameter Details below. Default: NO. |
reconnectKickEnable parameter details:
-
Set
reconnectKickEnable = trueIf another mobile device is already online when this device attempts to reconnect, 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 attempts to reconnect, the already-online device is kicked offline and this device comes online.
Example
- Swift
- Objective-C
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 ?? "")")
}
)
NCConnectParams *params = [[NCConnectParams alloc] initWithToken:@"your_token"];
params.reconnectKickEnable = YES;
[NCEngine connectWithParams:params
databaseOpenedHandler:nil
completionHandler:^(NSString *userId, NCError *error) {
// Handle connection result
}];