question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how to send an end event beforeunload?

See original GitHub issue

I want to send an end event like this:

window.addEventListener('unload',() => {
    store.dispatch({
        type: 'WEB_CHAT/SEND_EVENT',
        payload: {
          name: 'EndOfConversation'
        }
      });
});

but it didn’t work for me. As i know,unload will block xhr request,is there other way to resolve this problem?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
stevkancommented, May 18, 2020

@zp1112, I have found the following to work for me. Place it in your page where the Web Chat store is accessible. In my case, I’m persisting conversations which is how I’m able to see the posted message in Web Chat.

I can’t say that it will work in every situation for every browser or version (I’m using Chrome and have run successfully on a multitude of versions), but it’s a jumping off point, at least. @compulim could speak more to that, if more is needed to be said than what he posted above.

window.addEventListener( 'sendMessageActivity', ( { data } ) => {
  store.dispatch( {
    type: 'WEB_CHAT/SEND_MESSAGE',
    payload: {
      text: data
    }
} );

window.onbeforeunload = function() {
  const eventSendMessage = new Event( 'sendMessageActivity' );
  eventSendMessage.data = 'User left conversation';
  window.dispatchEvent( eventSendMessage );
}

Message received by bot

File Transcript Activity >> {
  direction: 'incoming',
  activity: {
    callerId: 'urn:botframework:azure',
    channelData: {
      clientActivityID: '15898261515331cqv08r1lb5',
      clientTimestamp: '2020-05-18T18:22:31.533Z'
    },
    channelId: 'directline',
    conversation: { id: 'JmXdzwRkQtE6ekG53YK95p-o' },
    from: {
      id: 'dl_15898261456610.9zj0c8ysgp',
      name: 'Lord Voldemort',
      role: 'user'
    },
    id: 'JmXdzwRkQtE6ekG53YK95p-o|0000003',
    locale: 'en-US',
    rawTimestamp: '2020-05-18T18:22:31.5733129Z',
    recipient: { id: 'b***g@QaeuoeEamLg', name: 'R***e Bot' },  
    serviceUrl: 'https://directline.botframework.com/',
    text: 'User left conversation',         <============ Text sent from Web Chat
    textFormat: 'plain',
    timestamp: 2020-05-18T18:22:31.573Z,
    type: 'message'
  }
}

Web Chat activity

image

Hope of help!!

0reactions
leijaecommented, Jul 3, 2021

this method doesn’t work. yes the message is added to the chat window, but newest version of chrome disables the request. “Send failure” is the result.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Window: beforeunload event - Web APIs | MDN
The beforeunload event is fired when the window, the document and its resources are about to be unloaded. The document is still visible...
Read more >
How to Send an Asynchronous Request at the End of a Page ...
First, we might think of the beforeunload event. It enables the web page to trigger a confirmation dialog to ask the user if...
Read more >
Sure you want to leave?—browser beforeunload event
In the video, I explain a bit about the beforeunload event—which lets you prompt or warn your user that they're about to leave...
Read more >
unload And beforeunload Events And How To Debug Them ...
So I set a breakpoint where the code that is supposed to be fired by the unload event is, and reloaded the page...
Read more >
How to call a function before leaving page with Javascript
The short answer is you can't do this. The onbeforeunload event can only be used to trigger that popup, it can't be used...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found