Skip to content
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

TypeError: this.$socket.$unsubscribe is not a function #431

Open
ieshan opened this issue Dec 13, 2019 · 12 comments
Open

TypeError: this.$socket.$unsubscribe is not a function #431

ieshan opened this issue Dec 13, 2019 · 12 comments
Labels
bug 🐛 Something isn't working

Comments

@ieshan
Copy link

ieshan commented Dec 13, 2019

Please take a look at the following link on App.vue line 38 and 39

Example Link

The $subascribe function does not exist.

@probil
Copy link
Owner

probil commented Dec 13, 2019

Try version 4.0.0
For some reason tests were unable to catch such an issue.

Thanks for your issue BTW 👍

@probil probil added the bug 🐛 Something isn't working label Dec 13, 2019
@ieshan
Copy link
Author

ieshan commented Dec 14, 2019

@probil seems like the issue exists in 4.0.0 too in a different way.

When the page is loaded the first time and the Vue instance is created() the $subscribe function was available, but when I moved to another page and destroyed() called the $unsubscribe it raised TypeError. Again when I moved to the (without reloading) page the $subscribe function was not available.

Could this be the cause of it?

Probable Cause

@steevepay
Copy link

I'm having the same issue. When I change the route and come back, the $subscribe is not available anymore.

@Lavhe
Copy link

Lavhe commented Mar 5, 2020

I am still facing this issue

@ross-crowdsmart
Copy link

I am also facing this issue. Will this be fixed soon?

@zburk
Copy link

zburk commented Mar 23, 2020

@probil Issue still not resolved. This is a pretty big issue as it renders the $subscribe and $unsubscribe useless

@probil probil pinned this issue May 25, 2020
@probil
Copy link
Owner

probil commented May 29, 2020

It seems like introducing this.$socket.$subscribe was a mistake. 🤔

For now you can attach events directly on the socket instance, e. g.:

// subscribe
this.$socket.client.on('eventName', () => {
    // some code here
})

// unsubscribe
this.$socket.client.off('eventName', () => {
   // some code here
})

@probil
Copy link
Owner

probil commented May 29, 2020

Will try to check other use cases and probably release the next version without this.$socket.$subscribe :|

@TBetcha
Copy link

TBetcha commented Aug 19, 2020

Has anyone found a solution for this? I am actively experiencing it.

@probil
Copy link
Owner

probil commented Aug 19, 2020

@TBetcha The solution is mentioned above
#431 (comment)

@19apoorv97
Copy link

19apoorv97 commented Oct 22, 2020

// App.vue
mounted(){
  this.$socket.client.off('userSubscription');
  //this.$socket.client.on('userSubscription', (payload) => {console.log(payload)});
},
// server
socket.emit('userSubscription','Send this message to only those who subscribed');

plz show me how to handle this on server side because even though i write this.$socket.client.off('userSubscription');
it is still printing on console

@probil
Copy link
Owner

probil commented Feb 22, 2021

@19apoorv97 There is one important thing I haven't mentioned. You need to pass the same function to .off(event_name, fn) in order to unsubscribe properly, .off(event_name) without passed function won't work. The reason for this is that there might be tons of other functions listening. Socket io needs to know which exact function you want to unsubscribe.

export default {
  methods: {
    onEventName() {},
  },
  mounted() {
    // subscribe
    this.$socket.client.on('eventName', this.onEventName)
  },
  beforeDestroy() {
    // unsubscribe
    this.$socket.client.off('eventName', this.onEventName)
  },
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants