I'm using Pusher with Laravel Echo to create presence channels for certain areas in my application. All my front-end routing is done using Vue Router.
When switching between routes, Vue router will load certain ponents based on my routes settings. It works great, however, Pusher doesn't automatically disconnect the users from these channels. It will happen only if I run a full page refresh, which is not what I want.
In my ponent, the js code to join a Pusher channel is inside mounted
, like this:
data() {
return {
users: [],
}
},
mounted() {
Echo.join('transaction.' + this.tid)
.here(users => {
this.users = users;
}
})
.joining(user => {
this.users.push(user);
})
.leaving(user => {
this.users.splice(this.users.indexOf(user), 1);
});
},
methods: {
// ...
},
How do I disconnect the users from the channels using Vue Router routing, without refreshing the page?
Thank you.
I'm using Pusher with Laravel Echo to create presence channels for certain areas in my application. All my front-end routing is done using Vue Router.
When switching between routes, Vue router will load certain ponents based on my routes settings. It works great, however, Pusher doesn't automatically disconnect the users from these channels. It will happen only if I run a full page refresh, which is not what I want.
In my ponent, the js code to join a Pusher channel is inside mounted
, like this:
data() {
return {
users: [],
}
},
mounted() {
Echo.join('transaction.' + this.tid)
.here(users => {
this.users = users;
}
})
.joining(user => {
this.users.push(user);
})
.leaving(user => {
this.users.splice(this.users.indexOf(user), 1);
});
},
methods: {
// ...
},
How do I disconnect the users from the channels using Vue Router routing, without refreshing the page?
Thank you.
Share Improve this question asked Apr 8, 2017 at 17:06 Alessandra FAlessandra F 1351 silver badge5 bronze badges 1-
Echo.leave('transaction.' + this.tid);
? – milo526 Commented Apr 8, 2017 at 17:17
1 Answer
Reset to default 7I believe that Vue Router may keep the ponent instance alive for performance reasons. Because of that, the websocket channel could still be connected even when routing between ponents.
To prevent this issue, you can manually leave the channel using Vue's ponent destroyed
method.
Since you are using Laravel's Echo, all you need to do is: Echo.leave('channel-name')
...
methods: {
// ...
},
destroyed() {
Echo.leave('transaction.' + this.tid);
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745176544a4615219.html
评论列表(0条)