Shared Workers

A shared worker is one thread that every same-origin page, iframe or worker connects to by constructing SharedWorker with the same URL, for example to share one WebSocket between tabs. It became Baseline Newly available in May 2026 with Chrome 1 for Android 148.

counter.js and the page script, opened as #Tab-A and #Tab-BJavaScript
// counter.js: each page arrives as a 'connect' event carrying a MessagePort
let connections = 0;
onconnect = ({ ports: [port] }) => {
  const id = ++connections;
  port.onmessage = ({ data }) => port.postMessage(`${data} is client #${id} of ${connections}`);
};
// page script
const { port } = new SharedWorker('counter.js');
port.onmessage = (e) => console.log(e.data);     // assigning onmessage calls port.start()
port.postMessage(location.hash.slice(1));
Output
Tab-A is client #1 of 1
Tab-B is client #2 of 2

The worker is never told that a tab closed, so send a goodbye message on pagehide and drop that port.