You must initialize the Chat SDK before using its features.
Initialization
You can initialize the SDK in the following steps:
1. Prepare an SDKAppID.
2. Call TencentCloudChat.create to initialize the SDK.
3. Add SDK event listeners.
The detailed steps are as follows.
Preparing an SDKAppID
To perform the initialization, you must have a correct SDKAppID.
The SDKAppID uniquely identifies a Tencent Cloud Chat account. We recommend you apply for a new SDKAppID for each application. Messages are naturally isolated and cannot communicate between different SDKAppID values.
In the Chat Console, you can view all your SDKAppID values, and you can click Create Application to create an SDKAppID.
Calling the initialization API
After performing the above steps, you can call TencentCloudChat.create to initialize the SDK.
API
TencentCloudChat.create(options);
The options parameter is of the Object type. It contains the following attribute values:
Name
Type
Description
SDKAppID
Number
SDKAppID of the chat app
proxyServer
String | undefined
WebSocket proxy server
Sample
import TencentCloudChat from'@tencentcloud/chat';
import TIMUploadPlugin from'tim-upload-plugin';
let options ={
SDKAppID:0// Replace 0 with the `SDKAppID` of your chat application when connecting.
};
// Create an SDK instance.
// The `TencentCloudChat.create()` method returns the same instance for the same `SDKAppID`.
// The SDK instance is usually represented by `chat`.
let chat = TencentCloudChat.create(options);
// Set the SDK log output level.
// 0 - Common level. We recommend you use this level during access as it covers more logs.
// 1 - Release level. We recommend you use this log level in a production environment.
This event is triggered when the SDK enters the ready status. When SDK is ready, you can call SDK APIs such as the message sending API to use various features of the SDK.
This event is triggered when the SDK enters the not ready status. When SDK is not ready, you cannot use SDK features such as message sending. To use them, you need to call the login API to drive the SDK into the ready status.
This event is triggered when the SDK receives a newly pushed one-to-one message, group message, group tip, or group system message. When this event occurs, you can traverse event.data to obtain the message list and render it to the UI.
letonMessageReceived=function(event){
// event.data - An array that stores Message objects - [Message]
This event is triggered when the SDK receives a notification for message modifications. When this event occurs, the message sender can traverse event.data to obtain the message list and update the content of the message with the same ID on the UI.
letonMessageModified=function(event){
// event.data - An array that stores modified Message objects - [Message]
This event is triggered when the SDK receives a notification indicating that messages are recalled. When this event occurs, the access side can traverse event.data to obtain data of the message recall list and render the recalled messages to the UI. For example, The peer end has recalled a message can be displayed if a message is recalled during a one-to-one conversation, and "XXX has recalled a message" can be displayed if a message is recalled during a group conversation.
letonMessageRevoked=function(event){
// event.data - An array that stores Message objects - [Message]
// The `isRevoked` attribute value of each Message object is `true`
This event is triggered when the current user is kicked offline. The access side can read event.data.type to display the reason of forcibly logged out.
TencentCloudChat.TYPES.KICKED_OUT_MULT_ACCOUNT - The user is forcibly logged out because the same account logs in from multiple webpages on the web client.
TencentCloudChat.TYPES.KICKED_OUT_MULT_DEVICE - The user is forcibly logged out because the same account logs in from multiple terminals.
TencentCloudChat.TYPES.KICKED_OUT_USERSIG_EXPIRED - The signature expired.
TencentCloudChat.TYPES.KICKED_OUT_REST_API - The user is forcibly logged out by the RESTful API.
This event is triggered when the network status changes. event.data.state indicates the current network status. The enumerated values are described as follows:
TencentCloudChat.TYPES.NET_STATE_CONNECTED: Already connected to the network.
TencentCloudChat.TYPES.NET_STATE_CONNECTING: Connecting. This often occurs when the SDK must reconnect due to network jitter. The message "The current network is unstable" or "Connecting..." can be displayed at the access side based on this status.
TencentCloudChat.TYPES.NET_STATE_DISCONNECTED: Disconnected. The message "The current network is unavailable" can be displayed at the access side based on this status. The SDK will continue to try to reconnect. If the user network recovers, the SDK will automatically synchronize messages.