-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Socket disconnects before the onBeforeUnload event is fired. #1451
Comments
A temporary workaround is to call 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.stopImmediatePropagation()
event.returnValue = ''
}
}) More information here: socketio/engine.io-client#658 |
While I have since moved my entire project to v2 to resolve my issue, I should thank you for your suggestion. I will take a look at this. I was unaware of the event.stopImmediatePropagation() method. |
Since [1], the socket is now closed when receiving the "beforeunload" event in the browser. This change was meant to fix a discrepancy between Chrome and Firefox when the user reloads/closes a browser tab: Firefox would close the connection (and emit a "disconnect" event, at the Socket.IO level), but not Chrome (see [2]). But it also closes the connection when there is another "beforeunload" handler, for example when the user is prompted "are you sure you want to leave this page?". Note: calling "stopImmediatePropagation()" was a possible workaround: ```js window.addEventListener('beforeunload', (event) => { event.preventDefault(); event.stopImmediatePropagation(); event.returnValue = 'are you sure you want to leave this page?'; }); ``` This commit adds a "closeOnBeforeunload" option, which controls whether a handler is registered for the "beforeunload" event. Syntax: ```js const socket = require('engine.io-client')('ws://localhost', { closeOnBeforeunload: false // defaults to true }); ``` [1]: ed48b5d [2]: socketio/socket.io#3639 Related: - #661 - #658 - socketio/socket.io-client#1451 Reference: https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event
Starting with const socket = io(url, {
closeOnBeforeunload: false
}); |
Using |
I have finally moved up to the latest versions of these packages while at the same time moving my project to Typescript.
Using the |
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, thedisconnect
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:
^3.1.2
3.1.0
^6.0.1
Stack where this is not an issue:
2.4.0
2.4.1
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
Expected behavior
The disconnect should not trigger until a user confirms navigating away.
Platform:
Additional context
I found what appeared to be an old option
But this does not seem to work.
The text was updated successfully, but these errors were encountered: