1

I'm using SalesForce Lightning Chat ESW version 5.0

When a user logs out from our site we want to clear all active chat sessions for that user.

Our implementation is basically:

  1. When the user logs out, the page is reloaded and we set a cookie called RememberToClearTheChat
  2. we initialize our chat by loading a Salesforce-script myOrganizationDomain/embeddedservice/5.0/esw.min.js and then we set up some config stuff like embedded_svc.settings.displayHelpButton = true; and then we do a call to embedded_svc.init with a bunch of params.
  3. Once embedded_svc.liveAgentAPI has been loaded, we check if the user has a cookie called RememberToClearTheChat and in that case we do embedded_svc.liveAgentAPI.endChat(); embedded_svc.liveAgentAPI.clearSession();

It works sometimes for some users - but in some cases it randomly throws errors.

The errors are thrown during clearSession(). They are thrown by the a script called aura_prod.js which salesforce loads internally. An example error is Uncaught Error: deleteSessionData requires two non-null arguments (domain, keys). Seems like the data is cleared halfway, and we end up in a semi broken state.

The desired result would be that the chat should be 'reset' to the default state, which is a minimized button that basically just says "start chat".

But the actual result is that the user sees an expanded chat window with an input field, but without a connected agent. So you can write messages that no one will see. And there's no UI avaiable to 'end chat' to get out of this faulty state.

Screenshot below: In this image you just see the default-label "Agent" - that's incorrect. In a functioning chat you should either be shown a "you have spot X in our queue" or you should see the name & avatar of a real agent.

Question: Should we clear the chat using a different method?

or do we need to wait for some specific event before running clearSession() ?

screenshot

3
  • I am also getting same error, how did u solve yours. deleteSessionData requires two non-null arguments
    – R15
    Commented Mar 14, 2022 at 13:23
  • I think I did. But looking at my old code I'm not quite sure how. We basically run the same code in OP before leaving the page AND while a new pageview is loading the chat (in that case we wait with a timer until embedded_svc is defined) Commented Mar 29, 2022 at 7:13
  • @R15 : posted a bit more as an Answer. Hopefully it helps. It's so stupid - but the 'solution' is simply to try 2 times and HOPE that the next time the chat is loaded you can clear the previous session Commented Mar 29, 2022 at 17:06

1 Answer 1

2

I'll answer my own question in case anyone finds this post.

We kept using the same methods mentioned in the OP

embedded_svc.liveAgentAPI.endChat(); embedded_svc.liveAgentAPI.clearSession();

but we called them during 2 separate events

  1. On leaving the chat - try to clear the current session (this works sometimes)
  2. On loading the chat - try to clear the previous session (this works sometimes)

Doing both of these means it'll work most of the time, but it is a flaky hack.

Step 1: Do this before leaving the current session

if (embedded_svc && embedded_svc.liveAgentAPI){
    document.cookie = "RememberToClearTheChat";
    clearChat();
    return;
}

Step2: Every time you load the chat check for that cookie - and if it exists - run this code within a setInterval function every 500ms or so for 10 seconds. And pray that it works.


if (typeof embedded_svc !== "undefined") {
        const foundLiveAgentApi = embedded_svc.liveAgentAPI;
        if (foundLiveAgentApi) {
            clearInterval(waitForLiveAgentInterval);
            clearChat(shouldLogOutAfterClearingChat);
        }
}

A flaky solution that might not be 100% effective but was good enough for us to ship since it's "better than nothing".

EDIT: I should mention that the variable liveAgentAPI is lazy loaded so you must wait for it to be created before running this code. We ended up polling for it every 500 ms with 30 attempts using setInterval and a counter.

2
  • It says liveAgentAPI is undefined, any idea what I could be doing wrong ? Commented Nov 15, 2022 at 15:30
  • @DaniyalNasir Lucky enough I remember this problem exactly! liveAgentAPI is lazy-loaded, it can take some time for it to be created. We wrote a setInterval function that ran every 500ms, up to 30 times - checking if liveAgentAPI exists. Then we run our code after it's we know it exists. Commented Nov 15, 2022 at 16:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .