probil / vue-socket.io-extended

:v::zap: Socket.io bindings for Vue.js and Vuex (inspired by Vue-Socket.io)
MIT License
629 stars 38 forks source link

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

Open ieshan opened 4 years ago

ieshan commented 4 years ago

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

Example Link

The $subascribe function does not exist.

probil commented 4 years ago

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

Thanks for your issue BTW :+1:

ieshan commented 4 years ago

@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 commented 4 years ago

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

Lavhe commented 4 years ago

I am still facing this issue

ross-crowdsmart commented 4 years ago

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

zburk commented 4 years ago

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

probil commented 4 years ago

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 commented 4 years ago

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

TBetcha commented 4 years ago

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

probil commented 4 years ago

@TBetcha The solution is mentioned above https://github.com/probil/vue-socket.io-extended/issues/431#issuecomment-636210150

19apoorv97 commented 3 years ago
// 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 commented 3 years ago

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