{"id":15388210,"url":"https://github.com/georapbox/pubsub","last_synced_at":"2025-07-25T23:40:52.111Z","repository":{"id":21195347,"uuid":"24505381","full_name":"georapbox/PubSub","owner":"georapbox","description":"Javascript implementation of the Publish/Subscribe pattern.","archived":false,"fork":false,"pushed_at":"2023-01-06T18:29:12.000Z","size":491,"stargazers_count":29,"open_issues_count":2,"forks_count":7,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-11-01T11:51:35.656Z","etag":null,"topics":["callback","event-emitter","events","javascript","publish","publish-subscribe","pubsub","pubsub-instance","subscribe","subscription","topic"],"latest_commit_sha":null,"homepage":"","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/georapbox.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2014-09-26T15:46:02.000Z","updated_at":"2024-03-27T21:40:51.000Z","dependencies_parsed_at":"2023-01-13T21:20:20.289Z","dependency_job_id":null,"html_url":"https://github.com/georapbox/PubSub","commit_stats":null,"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/georapbox%2FPubSub","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/georapbox%2FPubSub/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/georapbox%2FPubSub/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/georapbox%2FPubSub/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/georapbox","download_url":"https://codeload.github.com/georapbox/PubSub/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223681406,"owners_count":17184945,"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":["callback","event-emitter","events","javascript","publish","publish-subscribe","pubsub","pubsub-instance","subscribe","subscription","topic"],"created_at":"2024-10-01T14:55:57.046Z","updated_at":"2025-04-15T17:32:05.342Z","avatar_url":"https://github.com/georapbox.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![npm version](https://img.shields.io/npm/v/PubSub.svg)](https://www.npmjs.com/package/PubSub)\n[![npm license](https://img.shields.io/npm/l/PubSub.svg)](https://www.npmjs.com/package/PubSub)\n\n# PubSub\n\nJavascript implementation of the [Publish/Subscribe](http://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern) pattern.\n\n## Install\n\n### npm\n```sh\n$ npm install PubSub\n```\n\n## Usage\n\nThe library is exported in UMD, CommonJS, and ESM formats. You can import it the following ways:\n\n### Using ESM import statement\n\n```js\nimport PubSub from 'PubSub';\n```\n\n### Using CommonJS require statement\n\n```js\nconst PubSub = require('PubSub');\n\n// If you use a bundler like Webpack, you may need to import it the following way \n// as it might try to use the ESM module instead of the CommonJS.\nconst PubSub = require('PubSub').default; \n```\n\n### Old school browser global\n\n```html\n\u003cscript src=\"https://cdn.jsdelivr.net/npm/PubSub@latest/dist/PubSub.umd.min.js\"\u003e\u003c/script\u003e\n```\n\n## API\n\n* [PubSub](#PubSub)\n    * [new PubSub([options])](#new_PubSub_new)\n    * _instance_\n        * [.subscribe(topic, callback, [once])](#PubSub+subscribe) ⇒ \u003ccode\u003enumber\u003c/code\u003e\n        * [.subscribeOnce(topic, callback)](#PubSub+subscribeOnce) ⇒ \u003ccode\u003enumber\u003c/code\u003e\n        * [.publish(topic, [...data])](#PubSub+publish) ⇒ \u003ccode\u003eboolean\u003c/code\u003e\n        * [.publishSync(topic, [...data])](#PubSub+publishSync) ⇒ \u003ccode\u003eboolean\u003c/code\u003e\n        * [.unsubscribe(topic)](#PubSub+unsubscribe) ⇒ \u003ccode\u003eboolean\u003c/code\u003e \\| \u003ccode\u003estring\u003c/code\u003e\n        * [.unsubscribeAll()](#PubSub+unsubscribeAll) ⇒ \u003ccode\u003e[PubSub](#PubSub)\u003c/code\u003e\n        * [.hasSubscribers([topic])](#PubSub+hasSubscribers) ⇒ \u003ccode\u003eboolean\u003c/code\u003e\n        * [.subscribers()](#PubSub+subscribers) ⇒ \u003ccode\u003eobject\u003c/code\u003e\n        * [.subscribersByTopic(topic)](#PubSub+subscribersByTopic) ⇒ \u003ccode\u003earray\u003c/code\u003e\n        * [.alias(aliasMap)](#PubSub+alias) ⇒ \u003ccode\u003e[PubSub](#PubSub)\u003c/code\u003e\n    * _static_\n        * [.createInstance([options])](#PubSub.createInstance) ⇒ \u003ccode\u003e[PubSub](#PubSub)\u003c/code\u003e\n\n\u003ca name=\"new_PubSub_new\"\u003e\u003c/a\u003e\n\n### new PubSub([options])\nCreates a PubSub instance.\n\n#### Available options\n\n| Param | Type | Default | Description |\n| --- | --- | --- | --- |\n| immediateExceptions\u003csup\u003e1\u003c/sup\u003e | \u003ccode\u003eboolean\u003c/code\u003e | \u003ccode\u003efalse\u003c/code\u003e | Force immediate exceptions (instead of delayed exceptions). |\n\n\u003csup\u003e1\u003c/sup\u003e *Before version 3.6.0 PubSub would fail to deliver your topics to all subscribers if one or more failed (see issue [#4](https://github.com/georapbox/PubSub/issues/4)). As of version 3.6.0 PubSub handles this by delaying thrown exceptions by default. You can set `immediateExceptions` to `true` or any truthy value in order to maintain the stack trace for development reasons but this is not recommended for production.*\n\n## Public Methods\n\n\u003ca name=\"PubSub+subscribe\"\u003e\u003c/a\u003e\n\n### subscribe(topic, callback, [once]) ⇒ \u003ccode\u003enumber\u003c/code\u003e\nSubscribe to events of interest with a specific topic name and a\ncallback function, to be executed when the topic/event is observed.\n\n**Kind**: instance method of \u003ccode\u003e[PubSub](#PubSub)\u003c/code\u003e  \n**Returns**: \u003ccode\u003enumber\u003c/code\u003e - The topic's token  \n\n| Param | Type | Default | Description |\n| --- | --- | --- | --- |\n| topic | \u003ccode\u003estring\u003c/code\u003e |  | The topic's name |\n| callback | \u003ccode\u003efunction\u003c/code\u003e |  | Callback function to execute on event, taking two arguments:        - {*} data The data passed when publishing an event        - {object} The topic's info (name \u0026 token) |\n| [once] | \u003ccode\u003eboolean\u003c/code\u003e | \u003ccode\u003efalse\u003c/code\u003e | Checks if event will be triggered only one time |\n\n**Example**  \n```js\nconst pubsub = new PubSub();\n\nconst onUserAdd = pubsub.subscribe('user_add', (data, topic) =\u003e {\n  console.log('User added');\n  console.log('user data:', data);\n});\n```\n\u003ca name=\"PubSub+subscribeOnce\"\u003e\u003c/a\u003e\n\n### subscribeOnce(topic, callback) ⇒ \u003ccode\u003enumber\u003c/code\u003e\nSubscribe to events of interest setting a flag\nindicating the event will be published only one time.\n\n**Kind**: instance method of \u003ccode\u003e[PubSub](#PubSub)\u003c/code\u003e  \n**Returns**: \u003ccode\u003enumber\u003c/code\u003e - The topic's token  \n\n| Param | Type | Description |\n| --- | --- | --- |\n| topic | \u003ccode\u003estring\u003c/code\u003e | The topic's name |\n| callback | \u003ccode\u003efunction\u003c/code\u003e | Callback function to execute on event, taking two arguments:        - {*} data The data passed when publishing an event        - {object} The topic's info (name \u0026 token) |\n\n**Example**  \n```js\nconst pubsub = new PubSub();\n\nconst onUserAdd = pubsub.subscribeOnce('user_add', (data, topic) =\u003e {\n  console.log('User added');\n  console.log('user data:', data);\n});\n```\n\u003ca name=\"PubSub+publish\"\u003e\u003c/a\u003e\n\n### publish(topic, [data]) ⇒ \u003ccode\u003eboolean\u003c/code\u003e\nPublishes a topic **asynchronously**, passing the data to its subscribers.  \nAsynchronous publication helps in that the originator of the topics will not be blocked while consumers process them.  \nFor synchronous topic publication check `publishSync`.\n\n**Kind**: instance method of \u003ccode\u003e[PubSub](#PubSub)\u003c/code\u003e  \n**Returns**: \u003ccode\u003eboolean\u003c/code\u003e - Returns `true` if topic exists and event is published; otheriwse `false`  \n\n| Param | Type | Description |\n| --- | --- | --- |\n| topic | \u003ccode\u003estring\u003c/code\u003e | The topic's name |\n| [data] | \u003ccode\u003e...\\*\u003c/code\u003e | The data to be passed to its subscribers |\n\n**Example**  \n```js\nconst pubsub = new PubSub();\n\npubsub.publish('user_add', {\n  firstName: 'John',\n  lastName: 'Doe',\n  email: 'johndoe@gmail.com'\n});\n```\n\u003ca name=\"PubSub+publishSync\"\u003e\u003c/a\u003e\n\n### publishSync(topic, [data]) ⇒ \u003ccode\u003eboolean\u003c/code\u003e\nPublishes a topic **synchronously**, passing the data to its subscribers.\n\n**Kind**: instance method of \u003ccode\u003e[PubSub](#PubSub)\u003c/code\u003e  \n**Returns**: \u003ccode\u003eboolean\u003c/code\u003e - Returns `true` if topic exists and event is published; otheriwse `false`  \n\n| Param | Type | Description |\n| --- | --- | --- |\n| topic | \u003ccode\u003estring\u003c/code\u003e | The topic's name |\n| [data] | \u003ccode\u003e...\\*\u003c/code\u003e | The data to be passed to its subscribers |\n\n**Example**  \n```js\nconst pubsub = new PubSub();\n\npubsub.publishSync('user_add', {\n  firstName: 'John',\n  lastName: 'Doe',\n  email: 'johndoe@gmail.com'\n});\n```\n\u003ca name=\"PubSub+unsubscribe\"\u003e\u003c/a\u003e\n\n### unsubscribe(topic) ⇒ \u003ccode\u003eboolean\u003c/code\u003e \\| \u003ccode\u003estring\u003c/code\u003e\nUnsubscribes from a specific topic, based on the topic name,\nor based on a tokenized reference to the subscription.\n\n**Kind**: instance method of \u003ccode\u003e[PubSub](#PubSub)\u003c/code\u003e  \n**Returns**: \u003ccode\u003eboolean\u003c/code\u003e \\| \u003ccode\u003estring\u003c/code\u003e - Returns `false` if `topic` does not match a subscribed event; otherwise the topic's name  \n\n| Param | Type | Description |\n| --- | --- | --- |\n| topic | \u003ccode\u003estring\u003c/code\u003e \\| \u003ccode\u003enumber\u003c/code\u003e | Topic's name or subscription reference |\n\n**Example**  \n```js\nconst pubsub = new PubSub();\n\n// Unsubscribe using the topic's name.\npubsub.unsubscribe('user_add');\n\n// Unsubscribe using a tokenized reference to the subscription.\npubsub.unsubscribe(onUserAdd);\n```\n\u003ca name=\"PubSub+unsubscribeAll\"\u003e\u003c/a\u003e\n\n### unsubscribeAll() ⇒ \u003ccode\u003e[PubSub](#PubSub)\u003c/code\u003e\nClears all subscriptions whatsoever.\n\n**Kind**: instance method of \u003ccode\u003e[PubSub](#PubSub)\u003c/code\u003e  \n**Returns**: \u003ccode\u003e[PubSub](#PubSub)\u003c/code\u003e - The PubSub instance.  \n**Example**  \n```js\nconst pubsub = new PubSub();\n\npubsub.subscribe('message1', () =\u003e {});\npubsub.subscribe('message2', () =\u003e {});\npubsub.subscribe('message3', () =\u003e {});\npubsub.unsubscribeAll();\npubsub.hasSubscribers(); // -\u003e false\n```\n\u003ca name=\"PubSub+hasSubscribers\"\u003e\u003c/a\u003e\n\n### hasSubscribers([topic]) ⇒ \u003ccode\u003eboolean\u003c/code\u003e\nChecks if there are subscribers for a specific topic.\nIf `topic` is not provided, checks if there is at least one subscriber.\n\n**Kind**: instance method of \u003ccode\u003e[PubSub](#PubSub)\u003c/code\u003e  \n**Returns**: \u003ccode\u003eboolean\u003c/code\u003e - Returns `true` there are subscribers; otherwise `false`  \n\n| Param | Type | Description |\n| --- | --- | --- |\n| [topic] | \u003ccode\u003estring\u003c/code\u003e | The topic's name to check |\n\n**Example**  \n```js\nconst pubsub = new PubSub();\n\npubsub.on('message', data =\u003e console.log(data));\n\npubsub.hasSubscribers('message');\n// -\u003e true\n```\n\u003ca name=\"PubSub+subscribers\"\u003e\u003c/a\u003e\n\n### subscribers() ⇒ \u003ccode\u003eobject\u003c/code\u003e\nGets all the subscribers as a set of key value pairs that\nrepresent the topic's name and the event listener(s) bound.\n\n**Kind**: instance method of \u003ccode\u003e[PubSub](#PubSub)\u003c/code\u003e  \n**Returns**: \u003ccode\u003eobject\u003c/code\u003e - A readonly object with all subscribers.  \n**Note**: Mutating the result of this method does not affect the real subscribers. This is for reference only.  \n**Example**  \n```js\nconst pubsub = new PubSub();\n\npubsub.subscribe('message', listener);\npubsub.subscribe('message', listener);\npubsub.subscribe('another_message', listener);\n\npubsub.subscribers();\n// -\u003e Object { message: Array[2], another_message: Array[1] }\n```\n\u003ca name=\"PubSub+subscribersByTopic\"\u003e\u003c/a\u003e\n\n### subscribersByTopic(topic) ⇒ \u003ccode\u003earray\u003c/code\u003e\nGets subscribers for a specific topic.\n\n**Kind**: instance method of \u003ccode\u003e[PubSub](#PubSub)\u003c/code\u003e  \n**Returns**: \u003ccode\u003earray\u003c/code\u003e - A copy array of all subscribers for a topic if exist; otherwise an empty array  \n**Note**: Mutating the result of this method does not affect the real subscribers. This is for reference only.\n\n| Param | Type | Description |\n| --- | --- | --- |\n| topic | \u003ccode\u003eString\u003c/code\u003e | The topic's name to check for subscribers |\n\n**Example**  \n```js\nconst pubsub = new PubSub();\n\npubsub.subscribe('message', listener1);\npubsub.subscribeOnce('message', listener2);\npubsub.subscribe('another_message', listener1);\n\npubsub.subscribersByTopic('message');\n// -\u003e Array [{token: 0, once: false, callback: listener1()}, {token: 1, once: true, callback: listener2()}]\n\npubsub.subscribersByTopic('another_message');\n// -\u003e Array [{token: 2, once: false, callback: listener1()}]\n\npubsub.subscribersByTopic('some_message_not_existing');\n// -\u003e Array []\n```\n\u003ca name=\"PubSub+alias\"\u003e\u003c/a\u003e\n\n### alias(aliasMap) ⇒ \u003ccode\u003e[PubSub](#PubSub)\u003c/code\u003e\nCreates aliases for public methods.\n\n**Kind**: instance method of \u003ccode\u003e[PubSub](#PubSub)\u003c/code\u003e  \n**Returns**: \u003ccode\u003e[PubSub](#PubSub)\u003c/code\u003e - The PubSub instance.  \n\n| Param | Type | Description |\n| --- | --- | --- |\n| aliasMap | \u003ccode\u003eObject\u003c/code\u003e | A plain object that maps the public methods to their aliases. |\n\n**Example**  \n```js\nconst pubsub = new PubSub().alias({\n  subscribe: 'on',\n  subscribeOnce: 'once',\n  publish: 'trigger',\n  publishSync: 'triggerSync',\n  unsubscribe: 'off',\n  hasSubscribers: 'has'\n});\n```\n\n## Static methods\n\n\u003ca name=\"PubSub.createInstance\"\u003e\u003c/a\u003e\n\n### PubSub.createInstance([options]) ⇒ \u003ccode\u003e[PubSub](#PubSub)\u003c/code\u003e\nCreates a PubSub instance. This is an alternative way to create a new instance if you don't prefer using the `new` keyword.\n\n**Kind**: static method of \u003ccode\u003e[PubSub](#PubSub)\u003c/code\u003e  \n**Returns**: \u003ccode\u003e[PubSub](#PubSub)\u003c/code\u003e - The PubSub constructor.  \n**Example**  \n```js\nconst pubsub = PubSub.createInstance();\n```\n\n## Changelog\n\nFor API updates and breaking changes, check the [CHANGELOG](https://github.com/georapbox/PubSub/blob/master/CHANGELOG.md).\n\n## More about Publish/Subscribe pattern\n\n- [The Observer Pattern - Addy Osmani](https://addyosmani.com/resources/essentialjsdesignpatterns/book/#observerpatternjavascript)\n- [Publish–Subscribe pattern - Wikipedia](http://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern)\n\n## License\n\n[The MIT License (MIT)](https://github.com/georapbox/PubSub/blob/master/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgeorapbox%2Fpubsub","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgeorapbox%2Fpubsub","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgeorapbox%2Fpubsub/lists"}