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 properties:
Name
Type
Description
SDKAppID
Number
SDKAppID for the Instant Messaging Chat app
proxyServer
String | undefined
WebSocket proxy server (the mini-program platform does not support the use of IP addresses)
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 client needs to listen and handle
SDK_READY
This event is triggered when the SDK enters the ready status. When the 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 put 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 notification. 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 of message revocation. When this event occurs, the access side can traverse event.data to obtain data of the revoked messages list and render the recalled messages to the UI. For example, "The peer has recalled a message" can be displayed during a one-to-one conversation, and "XXX has recalled a message" can be displayed 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 SDK receives a notification that messages have been read by the peer after the message receiver successfully calls setMessageRead to report the read status. When this event occurs, the access side can traverse event.data to obtain the list of messages read by the peer and render them to the UI. For example, in a C2C Chat, messages sent by the user can be changed from 'unread' to 'read'.
letonMessageReadByPeer=function(event){
// event.data - An array that stores Message objects - [Message]
// The `isPeerRead` attribute value of each Message object is `true`
This event is triggered when the SDK receives message read receipts. When the message receiver calls sendMessageReadReceipt, the message sender will receive this event.
letonMessageReadReceiptReceived=function(event){
// event.data - An array that stores read receipts
This event is triggered when the SDK receives a notification of message extension updates. After successfully calling setMessageExtensions, both oneself and the peer user (in C2C) or group members (in Group) will receive this event.
This event is triggered when the SDK receives a notification that Message Extension is deleted. After successfully calling deleteMessageExtensions, both the sender and the recipient (in C2C Chat) or group members (in Group) will receive this event.
This event is triggered when the total unread message count of all conversations is updated. event.data is the current unread count of both one-on-one and group chats.
This event is triggered when conversations in a conversation group are updated (e.g., adding a conversation to the group or removing it from the group).