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.

Socket disconnects before the onBeforeUnload event is fired.

See original GitHub issue

Describe the bug I ensure that the user really meant to navigate away from the page by listening to the beforeUnload event on the window. However, before this is handled, the disconnect event is emitted before the user has a chance to make their choice of whether or not they want to stay on the page.

I would like to note that this issue does not happen when I downgrade to v2.

Stack where I have this issue:

  • socket.io-client ^3.1.2
  • socket.io 3.1.0
  • socket.io-redis ^6.0.1

Stack where this is not an issue:

  • socket.io-client 2.4.0
  • socket.io 2.4.1
  • socket.io-redis 5.4.0

Lastly, I am working within a Vue.js app, and so I do also work with the vue-socket.io package.

To Reproduce

window.addEventListener('beforeunload', (event) => {
      if (sessionHasStarted) {
        this.$socket.emit('test') // test emit to catch on the server. Does not appear on server - only the  disconnect event.
        event.preventDefault()
        event.returnValue = ''
      }
    })

Expected behavior The disconnect should not trigger until a user confirms navigating away.

Platform:

  • Device: MacBook Pro 2017
  • OS: macOS Big Sur 11.2.3

Additional context I found what appeared to be an old option

const socket = io(url, { 'sync disconnect on unload':false });

But this does not seem to work.

Issue Analytics

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

github_iconTop GitHub Comments

5reactions
darrachequesnecommented, May 17, 2021

Starting with socket.io-client@4.1.0, you can now disable the “beforeunload” handler with the closeOnBeforeUnload option:

const socket = io(url, {
  closeOnBeforeunload: false
});
2reactions
chuckntaylorcommented, May 24, 2021

I have finally moved up to the latest versions of these packages while at the same time moving my project to Typescript. I can confirm that with:

  • socket.io-client ^4.1.2
  • socket.io ^4.1.2
  • socket.io-redis 6.1.1

Using the closeOnBeforeunload: false option resolved my previous issues. Thank you kindly for this awesome package!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Send SocketIO message on unload or upon confirmation ...
Using onunload - it seems that doesn't work for me. I see that the socket event is being send by the browser, but...
Read more >
Socket killed when using onbeforeunload? - Google Groups
Hey everyone, I'm having a problem where if I use the onbeforeunload event and the user opts to "stay on this page", it...
Read more >
Is there a way to prevent socket from being closed ...
Unfortunately, in the Socket class an eventlistener that closes the connection is added to the “beforeunload” event, rather than the ...
Read more >
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 >
socket disconnect event doesn't work properly-node.js
Use onbeforeunload to signal when they are leaving, then just set a boolean so that socket.io doesn't trigger an error. window.onbeforeunload = function(){ ......
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