Skip to content

Instantly share code, notes, and snippets.

Created Jun 5, 2020
What would you like to do?
[browser]SharedWorker example
<!doctype html>
<script type="module">
const worker = new SharedWorker("./worker.js");
worker.port.addEventListener("message", ev => {
document.body.textContent = `connected: ${}`;
worker.port.start(); // spawm connect event to a shared worker
// shared state for each script url
// - reset after all page closed (all ServiceWorker instance terminated)
let counter = 0;
this.addEventListener("connect", ev => {
const port = ev.ports[0]; // port of each ServiceWorker instances
Copy link

bellbind commented Jun 5, 2020


(open multiple pages to check these counters)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment