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
fileUploadProxy
String | undefined
Image, video, and file upload proxy address (the mini-program platform does not support the use of IP addresses)
fileDownloadProxy
String | undefined
Image, video, and file download proxy address (the mini-program platform does not support the use of IP addresses)
Examples
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.
Please call this interface to listen for events before calling the login API to avoid missing events dispatched by the SDK.
API
chat.on(eventName, handler, context);
Name
Type
Description
eventName
String
Event names. All event names are stored in the TencentCloudChat.EVENT variable. If you need to view them, you can use console.log(TencentCloudChat.EVENT) to display all the events.
handler
Function
The method for handling events. When an event is triggered, this handler will be called to process it.
context
Object | undefined
The expected context in which the handler executes.
Event names. All event names are stored in the TencentCloudChat.EVENT variable. If you need to view them, you can use console.log(TencentCloudChat.EVENT) to display all the events.
handler
Function
The method for handling events. When an event is triggered, this handler will be called to process it.
context
Object | undefined
The expected context in which the handler executes.
List of events that the integration side needs to listen and handle
SDK_READY
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`