Which is the current preferred method to wake a BGSW? FCM or WebSocket polling?

In the case of FCM, is the popup page the supposed to send the API call to gcm as specified here? https://www.plasmo.com/blog/posts/firebase-cloud-messaging-chrome-extension
Firebase Cloud Messaging in a Chrome Extension with MV3
Step by Step Guide on Integrating FCM in a Manifest Version 3 Chrome Extension
12 Replies
lab
lab•2y ago
You will likely want to do it in BGSW If the flow started from popup, you should send a message from popup to bgsw, then initiate the gcm registration Note that registration can happens anytime, but onMessage handler must. be specified at the root level
alank
alankOP•2y ago
I may be misunderstanding, how can I send it in the BGSW if it's already inactive?
lab
lab•2y ago
oh so the API has 2 side to it - a message "register" and a listerener, ref docs for other: https://developer.chrome.com/docs/extensions/reference/gcm/ The register can be done anytime, but the listener once created at bgsw initial, will be registed at the internal web notification level API of the browser which is outside of BGSW's runtime itself When GCM evoke BGSW in the future, it basically wake the BGSW up via that event handler - note that all states instantiated outside of that callback will be killed so it's best to rehydrate everything within that handler
alank
alankOP•2y ago
Interesting, still not sure I'm understanding the full flow of the BGSW. Essentially when I see service worker (inactive) the BGSW can be woken up and rehydrated with the data of the prior inactive worker. If my service worker is stateless is this is a major issue?
lab
lab•2y ago
BGSW can be woken up and rehydrated with the data of the prior inactive worker
It's still stateless BTW. By rehydration, I meant reading data from storage or calling an API to instantiate everything needed. Basically, no global state created outside of the listener callback - they will all be discarded
alank
alankOP•2y ago
Do I need GCM in order to reinstantiate or will a simple call to the BGSW turn the service worker from inactive to active?
lab
lab•2y ago
depends on which context - from CS, you can't call bc the context it runs on is killed from popup or tab page - as long as the page are open, BGSW will keep running mb that's only for tab page I think
lab
lab•2y ago
but popup, I recall you still need to poke bgsw now and then. I recently released a keepAlive feature in persistent API: https://www.npmjs.com/package/@plasmohq/persistent
npm
@plasmohq/persistent
A couple of hacks to keep the BGSW alive in a library. Latest version: 0.0.3, last published: 5 days ago. Start using @plasmohq/persistent in your project by running npm i @plasmohq/persistent. There are no other projects in the npm registry using @plasmohq/persistent.
lab
lab•2y ago
^ it basically poke BGSW via the platform.getInfo call <- this keeps it awake
alank
alankOP•2y ago
Nice! Will look into that. Will this run even if the popup is closed?
lab
lab•2y ago
yeah since it runs in bgsw You need to add that code in bgsw not popup :d
alank
alankOP•2y ago
Ahh makes sense ty ty I think that's exactly what I was looking for
Want results from more Discord servers?
Add your server