please select
  • UIKit
  • SDK
  • Server APIs
Chat/
SDK/
Web/
SDK
  • Install Chat SDK
  • Initialize Chat SDK
  • Login and Logout
  • Client APIs
  • Changelog
  • Message
    • Overview
    • Send a Message
    • Receive a Message
    • Historical Message
    • Forward Messages
    • Modify a Message
    • Delete Messages
    • Clear History Message
    • Recall a Message
    • Send an Online Message
    • Message Read Receipt
    • Query Messages
    • Targeted Group Message
    • Do not Notify
    • Key-Value Extensions
    • Translation
  • Conversation
    • Overview
    • Conversation List
    • Get Conversations
    • Unread Count
    • Pin Conversations
    • Delete Conversations
    • Mark
    • Conversation Group
  • Group
    • Overview
    • Group Management
    • Group Profile
    • Group Member Management
    • Group Member Profile
    • Custom Group Attribute
    • Group Counter
  • Community Topic
    • Community Management
  • User Profile and Relationship Chain
    • User Profile
    • User Status
    • Friend Management
    • Friend Group
    • Block List
  • Guideline for Beginners
  • Console Guide
    • Creating and Upgrading an Application
    • Basic Configuration
    • Feature Configuration
    • Account Management
    • Group Management
    • Webhook Configuration
  • Product Introduction
    • Message Management
      • One-to-One Message
      • Message Storage
      • Offline Push
      • Group Message
      • Message Formats
    • Account System
      • Login Authentication
      • Online Status Management
    • Group Related
      • Group System
      • Group Management
    • User Profile and Relationship Chain
      • Profile Management
      • Relationship Chain Management
  • Purchase Guide
    • Billing Overview
    • Pricing
  • Error Codes

Login and Logout

Feature Description

Users can send and receive messages only after they have logged in to the Chat SDK. To log in to the Tencent backend server, a user needs to provide information, including the UserID and UserSig. For more information on these parameters, see Login Authentication.

Login

Note:
After the successful login, wait until the SDK enters the ready status (by listening to the TencentCloudChat.EVENT.SDK_READY event), and then call APIs that require authentication, such as sendMessage.
To support multi-instance login (allowing logging in to the same account concurrently on multiple webpages), log in to the Chat Console, find the target SDKAppID, and click Application > Configuration > Login and Message > Login Settings > Max Login Instances per User per Platform to configure the number of instances. The configuration will take effect in 5 minutes.
API
chat.login(options);
Parameter
The options parameter is of the Object type. It contains the following attribute values:
Name
Type
Description
UserID
String
User ID.
UserSig
String
The password with which the user logs in to the Chat console. It is essentially the ciphertext generated by encrypting information such as the UserID. For the detailed generation method, see Generating UserSig.
Returned value
Promise
Sample
let promise = chat.login({userID: 'your userID', userSig: 'your userSig'});
promise.then(function(imResponse) {
console.log(imResponse.data);
// Logged in successfully
if (imResponse.data.repeatLogin === true) {
// This indicates that the account is already logged in.
console.log(imResponse.data.errorInfo);
}
}).catch(function(imError) {
console.warn('login error:', imError);
});

Logout

This API is used to log out of the Chat SDK. It is usually called when you switch between accounts. This API clears the login status of the current account and all the data in the memory.
Note:
When calling this API, the instance publishes the SDK_NOT_READY event. In this case, the instance is automatically logged out and cannot receive or send messages.
Assume that the value of the Web configuration item in the Chat Console is greater than 1, and the same account has been used to log in to instances a1 and a2. After a1.logout() is executed, a1 will be automatically logged out and cannot receive or send messages, while a2 will not be affected.
Assume that the Online Web Instances is set to 2, and your account has been used to log in to instances a1 and a2. When you use this account to log in to instance a3, either a1 or a2 will be forcibly logged out. In most cases, the instance that first entered the login state is forcibly logged out. This is called kicked offline due to multi-account login. If a1 is forcibly logged out, a logout process is executed within a1 and the KICKED_OUT event is triggered. You can listen for this event and redirect to the login page when the event is triggered. At this time, a1 is forcibly logged out, whereas a2 and a3 can continue to run properly.
API
chat.logout();
Parameter
None
Returned value
Promise
Sample
let promise = chat.logout();
promise.then(function(imResponse) {
console.log(imResponse.data);
// Logged out successfully
}).catch(function(imError) {
console.warn('logout error:', imError);
});

Terminating

You can terminate SDK instances, including logout, disconnecting persistent connection, and releasing all resources.
API
chat.destroy();
Parameter
None
Returned value
Promise
Sample
chat.destroy().then(() => {
console.log('sdk destroyed');
});

Login Settings

By default, multi-account login is not supported. If you use an account that has been logged in on another page to log in on the current page, the account may be forcibly logged out on the other page, which will trigger the KICKED_OUT event. You can proceed accordingly after detecting the event through listening. Below is an example:
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);