Maintaining a persitant connection in a MV3 Chrome Extension
Asked Answered
I

1

10

I'm trying to build a chrome extension that updates its badge every time some event happens on my server- in this case, my server is receiving a webhook from Zoom and then informing my chrome extension that the event occurred. From there my chrome extension will update a counter on its' badge, to show that the event occured. I'm trying to build it according to the chrome extension Manifest V3 but I can't find any info on how to do this correctly.

To my understanding, in Manifest V3 all client-side scripts must run in a service worker. That service worker starts up, runs some code, and then shuts down again. I was planning to open a websocket in my background script, which would have been fine under manifest v2, but I don't think you can do that any more.

My question is: How do I maintain a connection to receive data in my chrome extension? When I say data in this case I really just mean an event informing my chrome extension to update the counter on its badge.

Are there other routes besides keeping a websocket open in the background? I've looked around at things like Google Cloud Messaging but I don't think that was intended for what I would like to use it for.

Immanuel answered 28/8, 2021 at 17:32 Comment(3)
Use Push API which will wake the worker. Alternatively, force the worker to be aliveFortis
@wOxxOm Thank you! I actually started doing this at one point but I thought it might not be the correct way to use Push API since most of the examples I've seen for its use are for push notifications.Immanuel
@wOxxOm By Push API do you mean chrome.gcm?Isolation
C
2

Try to use an offscreen API, it was introduced some time ago by Google. You just need to check in which browser version this API is available.

This would give you an opportunity on having long living WS connection and communicate with service worker e.g. by runtime API.

EDIT:

offscreen API is available since Chrome 109

Crackbrain answered 20/1, 2023 at 15:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.