Skip to content

Conversation

@Neet-Nestor
Copy link
Contributor

@Neet-Nestor Neet-Nestor commented May 17, 2024

Overview

There are many different APIs available for the two-way communication between service worker and web pages. According to the suggestion of web.dev, previously we used the simple BroadcastChannel API.
https://web.dev/articles/two-way-communication-guide

However, if the service worker has been killed by the browser, messages sent via the BroadcastChannel API cannot revive it. This causes unstable service worker life while users are using the webapp.

This PR replaces BroadcastChannel API with the more primitive Client API as we found that messages sent via Client API will revive a stopped service worker. This ensures the normal functioning of our keep-alive mechanism.

Primary Change

  • service_worker.ts:
    • Replace BroadcastChannel with Client API (navigator.serviceWorker.controller.postMessage() and client.postMessage())
    • Add clientRegistry to remember mapping between incoming messages and client ids
    • Add
  • Rename files (the export names are kept the same):
    • web_service_worker.ts -> service_worker.ts
    • service_worker.ts -> extension_service_worker.ts

Testing

The chat webapp is able to correctly keeping service worker alive after this change.

@Neet-Nestor Neet-Nestor requested review from rickzx and tqchen May 17, 2024 08:49
@Neet-Nestor Neet-Nestor changed the title [ServiceWorker][Feature] Fix service worker keep alive using "fetch" events [Feature] Replace BroadcastChannel API with Client API in Service Worker May 19, 2024
@tqchen tqchen merged commit dd5ec5c into mlc-ai:main May 19, 2024
CharlieFRuan added a commit that referenced this pull request May 21, 2024
### Changes
Main changes include:
- New model `Hermes-2-Pro-Mistral-7B` in `prebuiltAppConfig` via:
  - #390
- Various `index.js` and `index.js.map` post-processings to resolve
frontend compatibility issues with `require()` and `perf_hoooks`
  - #397
  - #406
- Catch WebGPU OOM error upon `reload()` and `CreateEngine()`:
  - #402
- Service Worker support (in addition to Extension Service Worker):
  - #395
  - #400
  - #401

### WASM Version
v0_2_34 as no change is required.

### TVMjs
TVMjs compiled at
apache/tvm@a5862a5,
with only one change in `tvm/web`:
apache/tvm#17005
atebites-hub pushed a commit to atebites-hub/web-llm that referenced this pull request Oct 4, 2025
…ker (mlc-ai#401)

## Overview

There are many different APIs available for the two-way communication
between service worker and web pages. According to the suggestion of
web.dev, previously we used the simple BroadcastChannel API.
https://web.dev/articles/two-way-communication-guide

However, if the service worker has been killed by the browser, messages
sent via the BroadcastChannel API cannot revive it. This causes unstable
service worker life while users are using the webapp.

This PR replaces BroadcastChannel API with the more primitive Client API
as we found that messages sent via Client API will revive a stopped
service worker. This ensures the normal functioning of our keep-alive
mechanism.

## Primary Change

- service_worker.ts:
- Replace `BroadcastChannel` with Client API
(`navigator.serviceWorker.controller.postMessage()` and
`client.postMessage()`)
- Add `clientRegistry` to remember mapping between incoming messages and
client ids
  - Add 
- Rename files (the export names are kept the same):
  - `web_service_worker.ts` -> `service_worker.ts`
  - `service_worker.ts` -> `extension_service_worker.ts`

## Testing

- https://chat.webllm.ai/
- `examples/service-worker`

The chat webapp is able to correctly keeping service worker alive after
this change.
atebites-hub pushed a commit to atebites-hub/web-llm that referenced this pull request Oct 4, 2025
### Changes
Main changes include:
- New model `Hermes-2-Pro-Mistral-7B` in `prebuiltAppConfig` via:
  - mlc-ai#390
- Various `index.js` and `index.js.map` post-processings to resolve
frontend compatibility issues with `require()` and `perf_hoooks`
  - mlc-ai#397
  - mlc-ai#406
- Catch WebGPU OOM error upon `reload()` and `CreateEngine()`:
  - mlc-ai#402
- Service Worker support (in addition to Extension Service Worker):
  - mlc-ai#395
  - mlc-ai#400
  - mlc-ai#401

### WASM Version
v0_2_34 as no change is required.

### TVMjs
TVMjs compiled at
apache/tvm@a5862a5,
with only one change in `tvm/web`:
apache/tvm#17005
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants