Skip to main content

Connection Status

The IMLib SDK provides the ConnectionStatusListener interface for monitoring IM connection status changes. By listening to these changes, you can implement different business logic or display user notifications.

Connection Status Listener Overview

The ConnectionStatusListener interface is defined as follows:

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

When the connection status changes, the IMLib SDK notifies developers through the onChanged() callback. Refer to the table below for status descriptions.

Status NameValueDescription
NETWORK_UNAVAILABLE-1Network unavailable
CONNECTED0Connection established
CONNECTING1Connecting
UNCONNECTED2Initial state before initiating connection
KICKED_OFFLINE_BY_OTHER_CLIENT3Current device logged out due to login on another device
TOKEN_INCORRECT4Triggered when Token expires
CONN_USER_BLOCKED6User banned via Console
SIGN_OUT12Manual disconnection state (see Disconnect)
SUSPEND13Temporary connection suspension (usually network-related). SDK will auto-reconnect.
TIMEOUT14Connection timeout. SDK stops retrying. Manual reconnection via Connect API required.

Setting Up the Listener

We recommend setting this up during the app lifecycle. Remove the listener when no longer needed to prevent memory leaks.

private RongIMClient.ConnectionStatusListener connectionStatusListener = new RongIMClient.ConnectionStatusListener() {
@Override
public void onChanged(ConnectionStatus status) {
// Implement business logic based on status codes
}
};
public void setIMStatusListener() {
RongIM.setConnectionStatusListener(connectionStatusListener);
}

Checking Current Connection Status

Use getCurrentConnectionStatus to actively retrieve the current IM connection state.

tip

If the SDK initiates reconnection when checking status, the returned state might temporarily show as "connected".

Sample Code

RongCoreClient.getInstance().getCurrentConnectionStatus()