{"id":22654315,"url":"https://github.com/sysix/broadcast-single-worker","last_synced_at":"2026-02-07T01:30:47.805Z","repository":{"id":244595455,"uuid":"815697926","full_name":"Sysix/broadcast-single-worker","owner":"Sysix","description":"Create a single worker for multiple browser tabs","archived":false,"fork":false,"pushed_at":"2024-06-17T20:21:32.000Z","size":106,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-12-01T06:53:39.510Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Sysix.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-15T21:47:22.000Z","updated_at":"2024-06-17T20:21:35.000Z","dependencies_parsed_at":null,"dependency_job_id":"1a2771ce-2010-4abc-9fca-4c76d4a190fb","html_url":"https://github.com/Sysix/broadcast-single-worker","commit_stats":null,"previous_names":["sysix/broadcast-single-worker"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/Sysix/broadcast-single-worker","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sysix%2Fbroadcast-single-worker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sysix%2Fbroadcast-single-worker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sysix%2Fbroadcast-single-worker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sysix%2Fbroadcast-single-worker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Sysix","download_url":"https://codeload.github.com/Sysix/broadcast-single-worker/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sysix%2Fbroadcast-single-worker/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29183939,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-07T00:44:15.062Z","status":"ssl_error","status_checked_at":"2026-02-07T00:35:01.758Z","response_time":59,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-09T09:49:13.586Z","updated_at":"2026-02-07T01:30:47.790Z","avatar_url":"https://github.com/Sysix.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# broadcast-single-worker\n\n![test](https://github.com/Sysix/broadcast-single-worker/actions/workflows/test.yml/badge.svg)\n[![NPM Version](https://img.shields.io/npm/v/%40sysix%2Fbroadcast-single-worker)](https://www.npmjs.com/package/@sysix/broadcast-single-worker)\n[![NPM License](https://img.shields.io/npm/l/%40sysix%2Fbroadcast-single-worker)](https://www.npmjs.com/package/@sysix/broadcast-single-worker)\n\n## Setup\n\nInstall with npm:\n\n`npm install @sysix/broadcast-single-worker`\n\nUse it in your code:\n\n```typescript\nimport BroadcastSingleWorker from '@sysix/broadcast-single-worker';\n\nconst worker = new BroadcastSingleWorker('channel_name');\n\nworker.addListener('start-worker', () =\u003e {\n    // this code will only be executed for a single tab\n});\n\nworker.addListener('stop-worker', () =\u003e {\n    // maybe you need to reset some listeners or timeouts\n});\n\n// connect to worker to the channel, other browser tab will be notified\n// this will trigger start-worker event for the current tab\n// this will trigger stop-worker event for the other tab with has the main worker\nworker.connect();\n\n// when you no longer want to listen for other tabs\n// this will also tell other tabs that maybe they need to be the main worker\nworker.disconnect();\n```\n\n\n## Why?\n\nOne of my projects needs to poll from the server in a small interval.  \nWhen multiple tabs are connected to the server, then every tab starts a request after the interval is reached.\n\nThis library makes it possible to reduce server requests with this following example:\n\n```typescript \nimport BroadcastSingleWorker from '@sysix/broadcast-single-worker';\n\nconst worker = new BroadcastSingleWorker('channel_name');\nconst UIChannel = new BroadcastChannel('channel_name_ui');\nlet intervalId: number | undefined;\n\nconst updateUi = (json: unknown) =\u003e {\n  document.body.innerText = JSON.stringify(json);\n}\n\nworker.addListener('start-worker', () =\u003e {\n    intervalId = window.setInterval(async () =\u003e {\n        const response = await window.fetch('https://host/poll-ui-changes');\n        const json = await response.json();\n\n        updateUi(json);\n        UIChannel.postMessage(json);\n    }, 30000);\n});\n\nworker.addListener('stop-worker', () =\u003e {\n    if (intervalId) {\n        window.clearInterval(intervalId);\n    }\n});\n\nUIChannel.onmessage = (message: MessageEvent\u003cunknown\u003e) =\u003e {\n    updateUi(message.data);\n}\n\nworker.connect();\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsysix%2Fbroadcast-single-worker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsysix%2Fbroadcast-single-worker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsysix%2Fbroadcast-single-worker/lists"}