probil/vue-socket.io-extended

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

ieshan opened this issue ยท 12 comments

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

Example Link

The $subascribe function does not exist.

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

Thanks for your issue BTW ๐Ÿ‘

@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

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

Lavhe commented

I am still facing this issue

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

zburk commented

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

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
})

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

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

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

// 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

@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)
  },
}