Establish Connection
The application client must successfully connect to RC servers before using the messaging functionality of RC's IM SDK.
When receiving a connection request from the client, RC's server will determine whether to allow the connection based on the user authentication token (Token parameter) carried in the request.
Prerequisites
- Obtain a Token through the server-side API [User Registration (Get Token)]. The RC client SDK does not provide methods for obtaining Tokens. Applications should call their own server to obtain Tokens from RC's servers.
- After obtaining the Token, the client can optionally save it 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.
- Token validity periods can be configured in the Console, with permanent validity as the default setting. Even when generating a new Token, the old unexpired Token remains valid. When a Token expires, you need to obtain a new one. If needed, you can actively call the server API Invalidate Token.
- It's recommended that applications set up connection status monitoring before connecting.
- The SDK must be initialized.
Do not call server APIs 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 and the app is decompiled, it could lead to leakage of the App Key and App Secret. Therefore, always ensure Tokens are obtained from your application server.
Connect to Chat Server
Determine the appropriate timing (login, registration, or other scenarios to prevent inability to access the app home page) to initiate a connection request to RC's chat server based on your application's business requirements and design.
- The
connectmethod must be called after SDK initialization. Otherwise, callbacks may not be received. - The SDK implements an automatic reconnection mechanism. When the IM connection is interrupted due to network issues, it will automatically reconnect until the application actively disconnects (see Disconnect).
Interface (With Timeout Setting)
For first-time connections to the chat server, it's recommended to use the interface with a connection timeout parameter (timeLimit) and set the timeout duration in seconds. In cases of poor network conditions causing connection timeouts, you can utilize the timeout error callback to notify users through UI prompts, such as suggesting they retry when network conditions improve.
Once successfully connected, the SDK's reconnection mechanism immediately takes effect and handles all reconnection processes. When disconnection occurs due to network issues, the SDK will continuously attempt reconnection until successful, requiring no additional connection operations from you. Active disconnection by the application will exit the reconnection mechanism (see Disconnect). For other scenarios, refer to Reconnection Mechanism and Reconnection Kick.
For password-free login, setting the timeLimit parameter to 0 allows page navigation immediately after the database opening callback (dbOpenedBlock), prioritizing display of local historical data while fully delegating connection logic to the SDK.
Interface Prototype
- (void)connectWithToken:(NSString *)token
timeLimit:(int)timeLimit
dbOpened:(nullable void (^)(RCDBErrorCode code))dbOpenedBlock
success:(nullable void (^)(NSString *userId))successBlock
error:(nullable void (^)(RCConnectErrorCode errorCode))errorBlock;
Parameter Description
| Parameter | Type | Description |
|---|---|---|
| token | NSString | Required for initial connection. Must be obtained by your app server calling RC server APIs. See server API documentation User Registration |
| timeLimit | int | SDK connection timeout duration in seconds |
| dbOpenedBlock | Block | Callback for local message database opening |
| successBlock | Block | Callback for successful connection establishment |
| errorBlock | Block | Callback for failed connection establishment |
-
timeLimitParameter Details:Parameter Value Range Description timeLimit ≤ 0 The SDK will continue connecting until successful or encountering unrecoverable errors (e.g., invalid token), equivalent to the connection interface above. timeLimit > 0 The SDK will attempt connection for up to timeLimit seconds. If timeout occurs, returns RC_CONNECT_TIMEOUT error and stops reconnecting. -
dbOpenedBlockDetails:Callback Parameter Callback Type Description code RCDBErrorCode Whether the database is opened -
successBlockDetails:Callback Parameter Callback Type Description userId NSString User ID of the successfully connected user -
errorBlockDetails:Callback Parameter Callback Type Description status RCConnectErrorCode Error code for connection failure. See Connection Status Codes below.
Example Code
[[RCCoreClient sharedCoreClient] connectWithToken:@"Token value obtained by developer's server through server API"
timeLimit:30
dbOpened:^(RCDBErrorCode code) {
//Message database opened, can proceed to main interface
} success:^(NSString *userId) {
//Connection successful
} error:^(RCConnectErrorCode errorCode) {
if (errorCode == RC_CONN_TOKEN_INCORRECT) {
//Token error, verify whether the App Key used for SDK initialization matches the one used by the app server to obtain the Token
} else if(errorCode == RC_CONNECT_TIMEOUT) {
//Connection timeout, show prompt suggesting users retry when network conditions improve
} else {
//Unable to connect to IM server, handle according to the specific error code
}
}]
Interface (Without Timeout Setting)
This interface is recommended when the client user has previously successfully logged into your application and connected to RC's chat server, and is now logging in offline.
Since the user already has historical data, there's no strict dependency on successful connection. You can navigate pages immediately after the database opening callback (dbOpenedBlock) to prioritize displaying local historical data, while fully delegating connection logic to the SDK.
This interface has been available since SDK version 5.20.0.
After calling this interface, the SDK's reconnection mechanism immediately takes effect and handles all reconnection processes. The SDK will continuously attempt reconnection until successful, requiring no additional connection operations from you. Active disconnection by the application will exit the reconnection mechanism (see Disconnect). For other scenarios, refer to Reconnection Mechanism and Reconnection Kick.
Interface Prototype
- (void)connectWithToken:(NSString *)token
dbOpened:(nullable void (^)(RCDBErrorCode code))dbOpenedBlock
success:(nullable void (^)(NSString *userId))successBlock
error:(nullable void (^)(RCConnectErrorCode errorCode))errorBlock;
Parameter Description
| Parameter | Type | Description |
|---|---|---|
| token | NSString | Requires your Server to access RC services to obtain Token |
| dbOpenedBlock | Block | Callback for local message database opening |
| successBlock | Block | Callback for successful connection establishment |
| errorBlock | Block | Callback for failed connection establishment |
-
dbOpenedBlockdescription:Callback Parameter Callback Type Description code RCDBErrorCode Whether the database is opened -
successBlockdescription:Callback Parameter Callback Type Description userId NSString User ID of the successfully connected user -
errorBlockdescription:Callback Parameter Callback Type Description status RCConnectErrorCode Error code for connection failure. See Connection Status Codes below for details.