Skip to main content

Connection

The application client can only use the message sending and receiving functions of RC's IM SDK after successfully connecting to RC's servers.

Upon receiving a connection request from the client, RC's server will determine whether to allow the user connection based on the user authentication token (Token parameter) carried in the connection request.

Prerequisites

  • Register users (obtain Token) through the server API. The RC client SDK does not provide methods for obtaining Tokens. Applications can call their own server to obtain Tokens from RC's server.

    • After obtaining the Token, the client can save it as needed for subsequent connections. The specific storage location depends on the application client design. If the Token remains valid, there's no need to request a new one from RC.
    • The Token validity period can be configured in the Console, with permanent validity as the default setting. Even if a new Token is regenerated, the old Token remains valid until expiration. Once expired, a new Token must be obtained.
  • It is recommended that applications set up connection status listeners before connecting.

  • The SDK must be fully initialized.

tip

Do not call the server API directly from the client to obtain Tokens. Obtaining a Token requires providing the App Key and App Secret. If these credentials are stored on the client side, they could be exposed if the application is decompiled. Therefore, always ensure Tokens are obtained from the application server.

Connecting to the Chat Server

Determine the appropriate timing (such as login, registration, or other moments to prevent inability to access the application homepage) to initiate a connection request to RC's chat server based on your application's business requirements and design.

Please note the following:

  • The connect() method must be called after SDK initialization. Otherwise, callbacks may not be triggered.

  • The SDK has its own reconnection mechanism, so there's no need to call connect() multiple times within an application lifecycle.

  • Call it once during the application lifecycle.

Use the connect method to initiate the connection, passing the user authentication token (Token).

Interface

RongIMLib.connect(token, reconnectKickEnable)


#### Parameter Description

| Parameter | Type | Required | Description |
|:--- |:--- |:--- |:--- |
| token | string | Yes | User authentication token (Token). |
| reconnectKickEnable | boolean | No | Determines whether to kick out the currently reconnecting device during reconnection (only effective in Electron environments). |


#### Example Code

```js
RongIMLib.connect('<Your-token>').then((res) => {
if (res.code === 0) {
console.log(res.data.userId)
}
})
<!-- Link Area -->
[Register users (obtain Token)]: /platform-chat-api/user/register