Socket disconnects before the onBeforeUnload event is fired.
See original GitHub issueDescribe 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:
- Created 3 years ago
- Reactions:2
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Starting with
socket.io-client@4.1.0
, you can now disable the “beforeunload” handler with thecloseOnBeforeUnload
option: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:
^4.1.2
^4.1.2
6.1.1
Using the
closeOnBeforeunload: false
option resolved my previous issues. Thank you kindly for this awesome package!