Skip to main content

Connection Status

The IMLib SDK provides the IM connection status listener ConnectionStatusListener for apps. By monitoring changes in IM connection status, you can implement different business logic or display prompts on the interface.

Connection Status Listener Description

The ConnectionStatusListener interface is defined as follows:

  public interface ConnectionStatusListener {
void onChanged(ConnectionStatus status);
}

When the connection status changes, the IMLib SDK will callback the current connection status to you through the onChanged() method. Refer to the table below for detailed descriptions of each status.

Status NameStatus ValueDescription
NETWORK_UNAVAILABLE-1Network unavailable.
CONNECTED0Connection successful.
CONNECTING1Connecting.
UNCONNECTED2Disconnected state, meaning the app hasn't called the connection method.
KICKED_OFFLINE_BY_OTHER_CLIENT3The user account logged in on another device, causing this device to be kicked offline.
TOKEN_INCORRECT4Triggered when the Token expires.
CONN_USER_BLOCKED6User banned via Console.
SIGN_OUT12User-initiated disconnection status. See Disconnect for details.
SUSPEND13Connection temporarily suspended (usually due to network issues). The SDK will automatically reconnect when appropriate.
TIMEOUT14Connection timeout. The SDK will stop connecting. Users need to handle the timeout and manually call the Connect API to reconnect.

Adding or Removing Connection Status Listeners

The SDK supports setting multiple listeners. It's recommended to set them during the app's lifecycle.

To prevent memory leaks, remove the listeners when they're no longer needed.

Sample Code

// Add connection status listener since 5.1.6
RongCoreClient.addConnectionStatusListener(listener);
// Remove connection status listener since 5.1.6
RongCoreClient.removeConnectionStatusListener(listener);

Getting Current Connection Status

You can actively retrieve the current IM service connection status using the getCurrentConnectionStatus method.

tip

When retrieving the connection status, if the SDK happens to start reconnecting, the returned status might still show as connected.

Sample Code

RongCoreClient.getInstance().getCurrentConnectionStatus()