Log in and Log out

Update time: 2024/03/14 18:45:31

Login and logout

  • During the connection process, the SDK will also execute related login logics. If login is successful, related login information can be returned in the onconnect callback of initialization configuration. If the login fails, related information can be returned in the ondisconnect callback.
  • To initialize the connection status, see "Initialize the SDK".


  • After "Initialize the SDK", the SDK will log in automatically.
  • After receiving onconnect callback, you can invoke nim.disconnect(); to log out SDK.
  • After logging out SDK, you can invoke nim.connect(); to log in SDK again.

Switching accounts

If you need to switch the IM account, you shall disconnect the Internet and then reconnect with a new account. Operation steps:

If the developer needs to make seamless switching, they can retain IM connection instances of different accounts and the upper-level developers manage different accounts.

javascript    var nim1 = NIM.getInstance({
        account: 'nim1',
        // ...
    var nim2 = NIM.getInstance({
        account: 'nim2',
        // ...
    // nim1 nim2 ... nimN will receive and send messages and have other IM features. Keep corresponding instance methods independent and browser performances operate stably

Update IM configuration

The SDK is designed as singleton pattern. If you need to update current IM configurations (some configurations cannot be updated, for example, user account), you can complete updates by the following two methods:

  • You can invoke NIM.getInstance again. You only need to input configurations to be updated (The method is not recommended. The improper call results in logic conflicts of instances).
  • You can invoke setOptions for original instances. The parameter list and format shall be consistent with NIM.getInstance. Updating token is taken for an example.
javascript// Disconnect IM
// Update a token
    token: 'newToken'
// Reconnect

Multi-device login

CommsEase supports multi-device login. Users can log in to the same account from mobile clients and web client simultaneously.

Initialization parameters

Sample codes

javascriptvar nim = NIM.getInstance({
    onloginportschange: onLoginPortsChange
function onLoginPortsChange(loginPorts) {
    console.log('The status of the current login account has changed on the other client', loginPorts);

Parameter description

  • onloginportschange: It is the callback of changing multi-device login status, and will receive Login client list. You can receive the callback when:
    • Other clients are online during login;
    • Other clients are online or offline after login.

Login client

Login client represents related information of a certain login device, with fields as below:

  • type: Device type for login
  • os: OS of login device
  • mac: mac address of login device
  • deviceId: Login device ID, uuid
  • account: Login account
  • connectionId: Connection account distributed by login device
  • ip: IP of login server
  • time: Login time
  • online: online or not.

Device types

Now, CommsEase supports several login clients as below:

  • 'Android' (Android)
  • 'iOS' (Apple)
  • 'PC' (Desktop. windows by default.)
  • 'Web' (Explorer)
  • 'Mac' (Desktop)

Validate previous login

Sample codes

    deviceIds: ['deviceId1'],
    done: onKick
function onKick(error, obj) {
    console.log('Remove other client’ + (!error?'succeeded':'failed'));

Parameter description

  • You can get device numbers at other login clients in onloginportschange callback. Please refer to Object of login client.
Was this page helpful?
  • Login and logout
  • Logout
  • Switching accounts
  • Update IM configuration
  • Multi-device login
  • Initialization parameters
  • Login client
  • Device types
  • Validate previous login