Support the ongoing development of Laravel.io →
posted 5 years ago

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?

Last updated 2 years ago.

jamieconnor liked this thread

1

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');

Last updated 4 years ago.
0

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.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.