SDKAppID
.TencentCloudChat.create
to initialize the SDK.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
.
TencentCloudChat.create
to initialize the SDK.TencentCloudChat.create(options);
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) |
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.chat.setLogLevel(0);// chat.setLogLevel(1);// Register the Tencent Cloud Chat upload plugin.chat.registerPlugin({'tim-upload-plugin': TIMUploadPlugin});
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. |
let onMessageReceived = function(event) {// event.name - TencentCloudChat.EVENT.MESSAGE_RECEIVED// event.data - An array to store Messages - [Message]};chat.on(TencentCloudChat.EVENT.MESSAGE_RECEIVED, onMessageReceived);
chat.off(eventName, handler, context, once);
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. |
once | Boolean | undefined | Whether to unbind only once. |
let onMessageReceived = function(event) {// event.name - TencentCloudChat.EVENT.MESSAGE_RECEIVED// event.data - An array to store Messages - [Message]};chat.off(TencentCloudChat.EVENT.MESSAGE_RECEIVED, onMessageReceived);
ready
status. When SDK is ready, you can call SDK APIs such as the message sending API to use various features of the SDK.let onSdkReady = function(event) {let message = chat.createTextMessage({to: 'user1',conversationType: TencentCloudChat.TYPES.CONV_C2C,payload: { text: 'Hello world!'}});chat.sendMessage(message);};chat.on(TencentCloudChat.EVENT.SDK_READY, onSdkReady);
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.let onSdkNotReady = function(event) {// chat.login({userID: 'your userID', userSig: 'your userSig'});};chat.on(TencentCloudChat.EVENT.SDK_NOT_READY, onSdkNotReady);
let onMessageReceived = function(event) {// event.data - An array that stores Message objects - [Message]};chat.on(TencentCloudChat.EVENT.MESSAGE_RECEIVED, onMessageReceived);
let onMessageModified = function(event) {// event.data - An array that stores modified Message objects - [Message]};chat.on(TencentCloudChat.EVENT.MESSAGE_MODIFIED, onMessageModified);
let onMessageRevoked = function(event) {// event.data - An array that stores Message objects - [Message]// The `isRevoked` attribute value of each Message object is `true`};chat.on(TencentCloudChat.EVENT.MESSAGE_REVOKED, onMessageRevoked);
let onMessageReadByPeer = function(event) {// event.data - An array that stores Message objects - [Message]// The `isPeerRead` attribute value of each Message object is `true`};chat.on(TencentCloudChat.EVENT.MESSAGE_READ_BY_PEER, onMessageReadByPeer);
let onMessageReadReceiptReceived = function(event) {// event.data - An array that stores read receiptsconst readReceiptInfoList = event.data;readReceiptInfoList.forEach((item) => {const { groupID, userID, messageID, readCount, unreadCount, isPeerRead } = item;// messageID - message ID// userID - receiver ID// isPeerRead - whether the message is read by peer// groupID - group ID// readCount - count of members read the message// unreadCount - count of members do not read the messageconst message = chat.findMessage(messageID);if (message) {if (message.conversationType === TencentCloudChat.TYPES.CONV_C2C) {if (message.readReceiptInfo.isPeerRead === true) {// message read by peer}} else if (message.conversationType === TencentCloudChat.TYPES.CONV_GROUP) {if (message.readReceiptInfo.unreadCount === 0) {// message read by all group members} else {// message.readReceiptInfo.readCount// If you want to find out who have read the message, please call getGroupMessageReadMemberList}}}});}chat.on(TencentCloudChat.EVENT.MESSAGE_READ_RECEIPT_RECEIVED, onMessageReadReceiptReceived);
let onMessageExtensionsUpdated = function(event) {const { messageID, extensions } = event.data;// messageID - message ID// extensions - list of message extensionsextensions.forEach((item) => {const { key, value } = item;});};chat.on(TencentCloudChat.EVENT.MESSAGE_EXTENSIONS_UPDATED, onMessageExtensionsUpdated);
let onMessageExtensionsDeleted = function(event) {const { messageID, keyList } = event.data;// messageID - message ID// keyList - list of keys which are deletedkeyList.forEach((key) => {// console.log(key)});};chat.on(TencentCloudChat.EVENT.MESSAGE_EXTENSIONS_DELETED, onMessageExtensionsDeleted);
let onConversationListUpdated = function(event) {console.log(event.data); // Array that stores Conversation objects - [Conversation]};chat.on(TencentCloudChat.EVENT.CONVERSATION_LIST_UPDATED, onConversationListUpdated);
let onTotalUnreadMessageCountUpdated = function(event) {console.log(event.data);};chat.on(TencentCloudChat.EVENT.TOTAL_UNREAD_MESSAGE_COUNT_UPDATED, onTotalUnreadMessageCountUpdated);
let onConversationGroupListUpdated = function(event) {console.log(event.data);}chat.on(TencentCloudChat.EVENT.CONVERSATION_GROUP_LIST_UPDATED, onConversationGroupListUpdated);
let onConversationInGroupUpdated = function(event) {const { groupName, conversationList } = event.data;}chat.on(TencentCloudChat.EVENT.CONVERSATION_IN_GROUP_UPDATED, onConversationInGroupUpdated);
let onGroupListUpdated = function(event) {console.log(event.data);// Array that stores Group objects - [Group]};chat.on(TencentCloudChat.EVENT.GROUP_LIST_UPDATED, onGroupListUpdated);
let onGroupAttributesUpdated = function(event) {const groupID = event.data.groupIDconst groupAttributes = event.data.groupAttributesconsole.log(event.data);};chat.on(TencentCloudChat.EVENT.GROUP_ATTRIBUTES_UPDATED, onGroupAttributesUpdated);
let onGroupCounterUpdated = function(event) {const { groupID, key, value } = event.data;};chat.on(TencentCloudChat.EVENT.GROUP_COUNTER_UPDATED, onGroupCounterUpdated);
let onTopicCreated = function(event) {const groupID = event.data.groupID; // community group IDconst topicID = event.data.topicID; // topic IDconsole.log(event.data);};chat.on(TencentCloudChat.EVENT.TOPIC_CREATED, onTopicCreated);
let onTopicDeleted = function(event) {const groupID = event.data.groupID; // community group IDconst topicIDList = event.data.topicIDList; // topic IDconsole.log(event.data);};chat.on(TencentCloudChat.EVENT.TOPIC_DELETED, onTopicDeleted);
let onTopicUpdated = function(event) {const groupID = event.data.groupID; // community group IDconst topic = event.data.topic; // the lastest topicconsole.log(event.data);};chat.on(TencentCloudChat.EVENT.TOPIC_UPDATED, onTopicUpdated);
event.data
is an array that stores Profile objects.let onProfileUpdated = function(event) {console.log(event.data); // Array that stores Profile objects};chat.on(TencentCloudChat.EVENT.PROFILE_UPDATED, onProfileUpdated);
let onUserStatusUpdated = function(event) {console.log(event.data);const userStatusList = event.data;userStatusList.forEach((item) => {const { userID, statusType, customStatus } = item;// userID// statusType, described as follows:// TencentCloudChat.TYPES.USER_STATUS_UNKNOWN// TencentCloudChat.TYPES.USER_STATUS_ONLINE// TencentCloudChat.TYPES.USER_STATUS_OFFLINE// TencentCloudChat.TYPES.USER_STATUS_UNLOGINED// customStatus})};chat.on(TencentCloudChat.EVENT.USER_STATUS_UPDATED, onUserStatusUpdated);
let onBlacklistUpdated = function(event) {console.log(event.data); // Your blocklist. The value is an array that stores `userID` values.};chat.on(TencentCloudChat.EVENT.BLACKLIST_UPDATED, onBlacklistUpdated);
let onFriendListUpdated = function(event) {console.log(event.data);}chat.on(TencentCloudChat.EVENT.FRIEND_LIST_UPDATED, onFriendListUpdated);
let onFriendGroupListUpdated = function(event) {console.log(event.data);}chat.on(TencentCloudChat.EVENT.FRIEND_GROUP_LIST_UPDATED, onFriendGroupListUpdated);
let onFriendApplicationListUpdated = function(event) {// friendApplicationList - Friend request list - [FriendApplication]// unreadCount - Number of unread friend requestsconst { friendApplicationList, unreadCount } = event.data;// Friend requests received by you (friend requests that are sent to you by others)const applicationSentToMe = friendApplicationList.filter((friendApplication) => friendApplication.type === TencentCloudChat.TYPES.SNS_APPLICATION_SENT_TO_ME);// Friend requests sent by you (friend requests that you send to others)const applicationSentByMe = friendApplicationList.filter((friendApplication) => friendApplication.type === TencentCloudChat.TYPES.SNS_APPLICATION_SENT_BY_ME);};chat.on(TencentCloudChat.EVENT.FRIEND_APPLICATION_LIST_UPDATED, onFriendApplicationListUpdated);
let onKickedOut = function(event) {console.log(event.data.type);//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.};chat.on(TencentCloudChat.EVENT.KICKED_OUT, onKickedOut);
let onNetStateChange = function(event) {console.log(event.data.type);//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 current network is unstable" or "Connecting..." can be displayed at the access side based on this status//TencentCloudChat.TYPES.NET_STATE_DISCONNECTED
// Disconnected. "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.};chat.on(TencentCloudChat.EVENT.NET_STATE_CHANGE, onNetStateChange);
chat.destroy();
Was this page helpful?