"Components in Pusher and Vue.js seem to be stuck in the channel even with Vue Router in

I've integrated Pusher with Laravel Echo to establish presence channels for specific areas within my application. All navigation on the front-end is managed through Vue Router. I'm facing an issue where Vue Router loads components based on route settings, but Pusher doesn't automatically disconnect users from these channels when switching routes. The disconnection only occurs if a full page refresh is triggered, which is not ideal. Within my component, the JavaScript code for joining a Pusher channel is in the mounted hook 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: {
        // ...
    },

Is there a way to disconnect users from the Pusher channels using Vue Router routing without having to refresh the entire page?

Appreciate any help or guidance. Thank you.

Answer №1

In my opinion, Vue Router may be keeping the component instance alive in order to optimize performance. This could potentially result in the websocket channel remaining connected even when switching between components.

To resolve this issue, you have the option to manually disconnect from the channel by utilizing Vue's component destroyed method.

For those using Laravel's Echo, all that is required is: Echo.leave('channel-name')


...
methods: {
    // ...
},
destroyed() {
    Echo.leave('transaction.' + this.tid);
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Looking for JavaScript code that can dynamically create an HTML table from JSON data

I am in need of a javascript solution that can dynamically generate either an HTML table or a bootstrap grid layout based on a specific data structure. [ {"x":0,"y":0,"width":2,"height":1,"title":"Lorem ipsum dolor sit amet"}, {"x":2,"y":0,"width ...

Using jQuery, prevent the user from entering text into an input box when a checkbox

In my project, there is a checkbox that determines whether an input box is disabled. The issue arises when I try to disable the input based on the database value. For example, when the value is 0, it should display as disabled false; however, the input rem ...

The combination of VueJS and Webpack Dev Server is having trouble hot reloading URL subpaths

My application operates within the subdirectory http://localhost:8080/admin_suffix The variable suffix is an ENV variable that I can modify and define in a .env file. After starting the webpack dev server, accessing http://localhost:8080/admin_suffix fun ...

A guide to effectively unit testing a Vue composition in isolation

In my project, I have organized my code in a similar way to the example in the documentation. This involves keeping my composition logic in a separate file from the component itself. Here is an overview of how it looks: // composition.js import { onMounte ...

The function _plugins_vuetify__WEBPACK_IMPORTED_MODULE_136__.default is not usable as a constructor

I have created a Vue application using vue cli 3 and incorporated Vuetify. To optimize the size of my bundle, I decided to modify the way Vuetify is imported: The versions I am working with are vuetify 1.5.5 and vue 3.7.0 import Vue from 'vue'; ...

Configuring JsFiddle with Vue and integrating vue-tables-2 - The variable "t" is not defined

Seeking assistance for implementing the vue-tables-2 package and encountering a persistent issue. Despite my efforts to set up a jsfiddle, I keep encountering an error stating "t is undefined" even with a simple implementation. Has anyone faced this specif ...

What is the best way to refresh a PHP function based on a JavaScript event?

I have a website where a div tag shows all the values from a SQL database using PHP. I want to reload this div tag with a JavaScript event, such as clicking on an HTML button. Is it possible to bring back all the values from the SQL database and display th ...

Issues arise with the functionality of Zurb Foundation 5 tabs

Utilizing the tabs feature in ZURB Foundation 5, I've noticed that clicking on a tab changes the hash in the URL. However, I actually want to prevent this behavior as I rely on the hash for managing page loads. Although I attempted to use preventDef ...

Using AJAX to call a PHP function within a PHP script

I have successfully implemented an AJAX file that can execute content from a PHP file. However, my concern is how to specifically call a particular function within the PHP file if there are multiple functions present. Below is my current AJAX code: $(doc ...

Ways to hide the value from being shown

I have integrated jscolor (from jscolor) to allow users to pick a color with an input field. However, I am facing issues in styling it as I'm unable to remove the hex value of the selected color and couldn't find any relevant documentation on av ...

Tips for incorporating `new google.maps.Marker()` with `vue2-google-maps` are as follows:1. First

Due to certain reasons, I find myself having to utilize new google.maps.Marker() with vue2-google-maps, but I'm unsure of where to begin as most documentation and tutorials use <GmapMarker ... /> in the HTML section instead. I've attempted ...

A plug-in for TinyMCE that allows users to adjust column widths within a table

We utilize TinyMCE in our content management system (CMS), and our users have expressed interest in being able to adjust the width of a column within a table. While HTML technically does not recognize columns, the Moodle editor HTMLAREA includes a plugin t ...

Angular does not update the progress bar

Imagine having a component html with your own customized round progress bar <round-progress #progress [current]="current" </round-progress> The "Current" value represents the percentage. In my TypeScript component, I have written: ...

Exclude the HTML attribute prior to displaying

Is there a way to prevent the src attribute from being used for each img tag until after a certain action is completed? I am trying to modify how images are fetched. Here's what I tried: $('img').each(function() { var elem = $(this); ...

Is there a way to locate the final element in a specific angular ng-repeat loop subset?

The final entry in the labels section may not be checked, making $last unsuitable for this scenario. This results in the following sequence: peaches, bananas, cherries, watermelon, How can I locate the last item in an angular ng-repeat loop while using an ...

The server will not accept the 'text/html' content type until it is terminated

I am encountering an issue with serving a simple html page through node. When I specify the content type as text/plain, the plain text of the html file loads correctly. However, when I switch it to "content-type" : "text/html", the browser tab keeps loadi ...

Unable to set the height for ul/div elements

I'm struggling to make my navbar appear when I click the "menu button". The div seems to be present in the code, but with a height of 0. Here's the relevant section of the code causing the issue. Any suggestions on how I can resolve this? var ...

Implementing input field validation before triggering the Bootstrap Modal in a Vue Laravel application

This Laravel Blade page contains: <!-- Main Container --> <main id="main-container"> <!-- Page Content --> <div class="content" id="app"> <!-- Lock Forms --> <!-- <h ...

Set a dynamic Active Class on various divisions by utilizing their respective indexes

I have two divs with the same class. When I click on an anchor tag within one of the elements, I want to add a class to the corresponding position in the second div as well. Mirror: JSFiddle Here is the jQuery code snippet: $(document).on('click ...

Is there a way to check if VueJs /Vuetify is currently in the middle of an animation?

When working with Angular and Jquery, it's easy to determine if something on the page is currently animating. By using the css selector ".ng-animating", you can check if any elements have the animating class applied to them. We rely on this functiona ...