I am developing MPA with Vue.js as the frontend and Laravel as the backend. I need real-time features in only one page (one Vue js component). I followed laravel documentation and I have events, broadcasting, Echo and Pusher all in place and working well. My problem is efficiency. I have Pusher.logToConsole set to true and I noticed that when I load any page in my app, it actually starts a Pusher instance and establishes a connection. I want this to happen only in the vue component that uses real-time features.
This is the code related to Pusher/Echo in my bootstrap.js:
import Echo from 'laravel-echo';
const client = require('pusher-js');
Pusher.logToConsole = true;
window.Echo = new Echo({
broadcaster: 'pusher',
key: 'my key is here',
cluster: 'eu',
clinet: client,
});
This is the code in the Vuejs component with real-time functionality:
Echo.private('my-channel')
.listen('.App\\Events\\myEvent', (e) => {
console.log(e);
});
Is there a way to instantiate Pusher and establish a connection only in the Vue component of interest?