Skip to content

Socket disconnects before the onBeforeUnload event is fired. #1451

Closed
@chuckntaylor

Description

@chuckntaylor

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions