Log out a user from the live chat

Learn how to log out a user from the Customerly Messenger using the customerly.logout() function, including details on its synchronous operation and the data it affects.

Luca Micheli
Written by Luca MicheliLast update 4 months ago

If you need to log out a user from the live chat on your website, Customerly provides a straightforward method to achieve this using the customerly.logout() function. Here’s a detailed guide on how this function works and how to use it effectively.

Using the customerly.logout() Function

To log out a user from the Customerly Messenger, you can use the following code:

customerly.logout();

This function is designed to be simple yet effective, and it is important to understand how it operates to use it correctly.

Key Features of customerly.logout()

  1. Synchronous Operation: The customerly.logout() function is synchronous. This means it executes its task immediately without waiting for any asynchronous processes to complete. You call it, and it does its job right away.

  2. Data Handling: When customerly.logout() is invoked, it performs the following actions:

    • Deletes cookies and various tokens associated with the user's session.

    • Calls the ping function again to ensure the session is fully terminated.

Understanding the Ping Function

  • Automatic Call: After logging out, the ping function is automatically called to refresh the session state. If this call doesn’t happen immediately because another process is running, it will simply be called later without the tokens, effectively ending the session.

  • Session Termination: The ping ensures that the user is logged out even if the initial call doesn’t complete due to other processes. This makes the logout process reliable and thorough.

Practical Example

Here’s a practical example of how you might implement the customerly.logout() function in your application:

function userLogout() {
  // Log out the user from Customerly Messenger
  customerly.logout();
  
  // Additional logout procedures can be added here
  console.log("User has been logged out from the live chat.");
}

// Call the logout function on a specific event, like a button click
document.getElementById("logoutButton").addEventListener("click", userLogout);

In this example, the userLogout function is called when a user clicks a logout button, effectively logging them out of the Customerly Messenger and performing any additional cleanup required by your application.

Did this answer your question?