Keeping a service worker w/websocket alive

696 views
Skip to first unread message

Don Schmitt

unread,
May 30, 2024, 11:44:10 AM5/30/24
to chromium-extensions
So we have this scenario:

1. A page rendering data that arrives from a websocket that may be shared between pages, so:
2. The websocket is opened and maintained by the extension service worker.

I need that websocket to stay open as long as any page that is using it is open so the page can continue to work correctly, so I need the service worker to stay active.  Some approaches:

1. I was using setInterval / 10s in the page to send messages to the service worker which would then send a packet over the websocket to keep it alive.  However, it turns out that if the page is not  the active tab, Chrome quits calling the interval (I was not aware a browser could do that), so the messages stop being sent and the service worker is unloaded.

2. I tried a long-lived port between the page and the service worker, but apparently you have to send periodic messages over that port to keep the service worker active, so setInterval in the page still won't work.

3. setInterval in the service worker to send messages over the long-lived port or the websocket _seems_ to work to keep the service worker alive.  However, I feel like I may just be getting lucky and the browser could decide to not call the interval in the service worker.

Suggestions for a reliable solution?



woxxom

unread,
May 30, 2024, 2:18:30 PM5/30/24
to Chromium Extensions, Don Schmitt
#3 is not luck, the timers in the extension's pages aren't throttled by design, but why specifically do you need the background script constantly running instead of saving the state in storage or in the content script?

Os

unread,
May 30, 2024, 2:38:06 PM5/30/24
to Don Schmitt, Chromium Extensions, Don Schmitt

Beware that Chromium team ominously reserves the right to disallow keeping the background script alive per https://developer.chrome.com/docs/extensions/develop/migrate/to-service-workers#keep-sw-alive

On 30-May-24 21:32, Don Schmitt wrote:
>>>> 3 is not luck, the timers in the extension's pages aren't throttled by design, 

Thanks for the clarification.

>>>> but why specifically do you need the background script constantly running instead of saving the state in storage or in the content script?

We only need it running as long as one of our extension pages (not a content script)  is loaded displaying data from the websocket connection.  The websocket might be shared by multiple tabs / pages so it lives in the service worker.



--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/d4d830b8-2ac7-4e15-908e-131b50861028n%40chromium.org.

Don Schmitt

unread,
May 30, 2024, 2:56:52 PM5/30/24
to Os, Chromium Extensions
Thanks for the heads-up, but we are not trying to keep the service worker alive continuously.

woxxom

unread,
May 30, 2024, 3:02:02 PM5/30/24
to Don Schmitt, Chromium Extensions

You keep it as long as the tab is open, right? So if the tab is always open it means the background script keeps running forever. The intentionally vague way this warning is phrased makes me err on the side of caution.

Reply all
Reply to author
Forward
0 new messages