{"id":16513778,"url":"https://github.com/wintercounter/highway.js","last_synced_at":"2025-10-28T04:32:23.903Z","repository":{"id":57171345,"uuid":"52470974","full_name":"wintercounter/Highway.js","owner":"wintercounter","description":"A flexible pub/sub event emitter for web-worker cross-communication with fallback support.","archived":false,"fork":false,"pushed_at":"2017-11-20T09:10:38.000Z","size":832,"stargazers_count":8,"open_issues_count":0,"forks_count":2,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-02-05T09:41:29.335Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://doclerlabs.github.io/Highway.js","language":"JavaScript","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/wintercounter.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}},"created_at":"2016-02-24T20:07:10.000Z","updated_at":"2017-11-21T04:16:27.000Z","dependencies_parsed_at":"2022-08-27T13:11:42.267Z","dependency_job_id":null,"html_url":"https://github.com/wintercounter/Highway.js","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wintercounter%2FHighway.js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wintercounter%2FHighway.js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wintercounter%2FHighway.js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wintercounter%2FHighway.js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wintercounter","download_url":"https://codeload.github.com/wintercounter/Highway.js/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238597386,"owners_count":19498396,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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-10-11T16:10:16.640Z","updated_at":"2025-10-28T04:32:17.816Z","avatar_url":"https://github.com/wintercounter.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Highway.js\nA flexible pub/sub event emitter for web-worker (and more) cross-communication with fallback support.\nWorker can be anything: an Iframe, a WebWorker, a ServiceWorker, a Node WS Server.\n\n![\"Highway PubSub Cross-Communication\"](/www/cross.png)\n\n## Usage\nInclude **Highway.js** on both client side and worker side.\n\nImport Highway and a Proxy you want to use, both in client side and worker side:\n```js\nimport Highway from 'Highway'\nimport WebWorkerProxy from 'Highway/Proxy/WebWorker'\n```\n\nInitialize:\n```js\n// Client side\nconst Host = self.Worker ? new self.Worker('worker.bundle.js') : self\nself.HW = self.HW || new Highway(new WebWorkerProxy(Host))\n// Worker side\nself.HW = self.HW || new Highway(new WebWorkerProxy(self))\n```\nFallback when there is no worker support:\n```javascript\n// On client side\nif (!self.Worker) {\n    const script = document.createElement('script')\n    script.setAttribute('src', 'worker.bundle.js')\n    document.body.appendChild(script)\n}\n```\n\nIn most cases you want to initialize client side stuff after the worker is loaded.\nUse your own event for this.\nFor example you need your data model from the backend before rendering the site.\n\n```js\n// Client side\nHW.one('ready', InitSomeGUIFramework)\n// Worker side\nInitSomeDataModelStuff()\nInitSomeWebSocketConnection()\nHW.pub('ready')\n```\n\n## API\n\n### HW.sub(string eventName, callable callback [,boolean one = false])\n\nSubscribe to an event.\n\n```js\nHW.sub('MyOwnEvent', function(){\n    // Do something when event occurs\n    // See pub, this will run 3 times\n})\nHW.sub('MyOwnEvent-\u003eDidSomething', function(){\n    // Do something when event occurs\n    // See pub, this will run once\n})\n```\n\n### HW.pub(eventName[, customData, customState])\n\nPublish an event.\n\n```js\nHW.pub('MyOwnEvent', { customData })\nHW.pub('MyOwnEvent-\u003eDidSomething', { customData })\nHW.pub('MyOwnEvent-\u003eIHaveState', { customData }, 'passed')\n```\n\n### HW.one(eventName, callback)\n\nSubscribe to an event, unsubscribe once it is called. It's a shorthand for\n`HW.sub(eventName, callback, true)`\n\n```js\nHW.one('MyOwnEventOnce', function(){\n    // I'll just run once and automatically unsubscribe then\n})\n```\n\n### HW.exe(callable)\n\nExecute a function on the other side. Your function will be executed within `self` context.\n\n```javascript\nHW.exe(() =\u003e {\n    // Do some code\n})\n```\n\n#### Disable exe\n\n```js\nHW.AllowExe = false\n```\n\n\u003e You might want to disable this if you're using Node as you backend for example.\n\u003e Function is passed over as a string and it's executed by `eval` which is a high security risk.\n\n### off\n\nUnsubscribe from an event.\n\n```javascript\n// Unsubscribe from ALL events associated with MyOwnEvent\nHW.off('MyOwnEvent')\n// Unsubscribe only a specific callback\nHW.off('MyOwnEvent', callback)\n// Unsubscribe from ALL events associated with MyOwnEvent, even the deep ones. eg: MyOwnEvent-\u003eDeepEvent too\nHW.off('MyOwnEvent', true)\n```\n\n### destroy\n\nDestroy the Highway instance.\n\n```javascript\nHW.destroy()\n```\n\n## Proxies\n- EventEmitter: for everything which uses the node EventEmitter interface (like socket.io)\n- Iframe\n- WebWorker: for both Web and Service Workers\n\n# Credits\n[Victor Vincent](http://wintercounter.me)\n\n![](http://c.statcounter.com/10870964/0/443694a8/1/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwintercounter%2Fhighway.js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwintercounter%2Fhighway.js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwintercounter%2Fhighway.js/lists"}