I managed to use Laravel Echo to receive notifications in my Vue project, but when I close the browser, it stops working.
I created a service-worker to use Echo there, but I can't use import Echo from 'laravel-echo' because I get the following error: Cannot use import statement outside a module
Is there a way to import Laravel-Echo in a service worker?
Ok, so it is possible to put Echo in a service worker, just import it as you normally would
import Echo from 'laravel-echo';
importScripts(
'pusher.worker.min.js',
)
const echo = new Echo({
broadcaster: 'pusher',
key: process.env.MIX_PUSHER_APP_KEY,
cluster: process.env.MIX_PUSHER_APP_CLUSTER,
encrypted: true
});
you'll need to tell webpack to compile it as well. Note: it is best to have the service worker compiled to the root directory.
I'm using mix so my webpack.mix.js has this in it
mix.js('resources/js/app.js', 'public/js')
.js('resources/js/service-worker.js', 'public')
.sass('resources/sass/app.scss', 'public/css');
I don't think you will be able to do what your wanting, because you will find the service worker will become 'activated and stopped' after a few minutes of the site being closed, so it will not be listening for push events.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community