tencent cloud

Feedback

JavaScript

Last updated: 2024-10-30 11:37:39

    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);
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support