What to do if the RC Client SDK's connect method returns an invalid user token

Cause 1: App Key and token mismatch in your project

Your token doesn’t match the App Key in the SDK. Verify that the locally configured App Key matches the one used to generate the token on the App Server side. Check the token’s validity in the RC Developer Console.

Cause 2: Token expired

Set the token’s validity period in the Developer Console. If it’s not set to permanent, request a new token from RC via your app’s server when you get a Token error (31004). Then, reconnect with the new token.

Cause 3: App Key mismatch between project and App Server

The App Key in your project doesn’t match the one on the App Server, causing the token to mismatch the client’s App Key.

Cause 4: App Secret refreshed

Refreshing the App Secret in the RC Developer Console invalidates all existing tokens. Each user must request a new token to connect.

Pseudocode example

When connect()’s onError() returns RC_CONN_TOKEN_INCORRECT (31004), fetch a new token from your server and reconnect. Here’s the pseudocode:

RongIMClient.connect("User Token", new RongIMClient.ConnectCallback() {
    @Override
    public void onDatabaseOpened(RongIMClient.DatabaseOpenStatus code) {
        // Message database opened, proceed to the main page
    }

    @Override
    public void onSuccess(String s) {
        // Connection successful
    }

    @Override
    public void onError(RongIMClient.ConnectionErrorCode errorCode) {
        if(errorCode.equals(RongIMClient.ConnectionErrorCode.RC_CONN_TOKEN_INCORRECT)) {
            // Fetch a new token from the App Server and reconnect (pseudocode)
            AppServer.getUserToken(() --> RongIMClient.connect(newToken));
        } else {
            // Handle other connection errors based on the error code
        }
    }
})