{"id":15819521,"url":"https://github.com/softwarespot/pubsub","last_synced_at":"2025-03-14T23:31:54.079Z","repository":{"id":58231086,"uuid":"41435816","full_name":"softwarespot/PubSub","owner":"softwarespot","description":"A publish-subscribe messaging pattern for JavaScript","archived":false,"fork":false,"pushed_at":"2017-03-12T09:10:07.000Z","size":166,"stargazers_count":5,"open_issues_count":4,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-09T19:09:15.328Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://softwarespot.github.io/PubSub/","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/softwarespot.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":"2015-08-26T16:10:14.000Z","updated_at":"2023-03-10T10:28:35.000Z","dependencies_parsed_at":"2022-08-30T20:01:42.738Z","dependency_job_id":null,"html_url":"https://github.com/softwarespot/PubSub","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/softwarespot%2FPubSub","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/softwarespot%2FPubSub/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/softwarespot%2FPubSub/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/softwarespot%2FPubSub/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/softwarespot","download_url":"https://codeload.github.com/softwarespot/PubSub/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243663516,"owners_count":20327300,"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-05T06:41:33.742Z","updated_at":"2025-03-14T23:31:49.069Z","avatar_url":"https://github.com/softwarespot.png","language":"JavaScript","readme":"# PubSub - v2.2.4\n\nAn easy to use publish subscribe module, based on the mediator pattern.\n\n## What exactly is PubSub?\n\nPubSub is a JavaScript module based around the Publish–Subscribe pattern. If you're unfamiliar with the Publish-Subscribe pattern, then please familiarise yourself by visiting the [MSDN](https://msdn.microsoft.com/en-us/library/ff649664.aspx) article about `PubSub`. You will be amazed as to why you didn't know this before and how you can't live without it.\n\n## How to use\n\n```html\n    \u003c!--Use the minified version for better performance--\u003e\n    \u003cscript src=\"dist/PubSub_es5.min.js\"\u003e\u003c/script\u003e\n\n    \u003cscript\u003e\n        // Call the following function when\n        function subscribed() {\n            // Display the alert when the 'onrefresh' is published to\n            window.alert('The \"onrefresh\" subscription was published to.');\n        }\n\n        // Register a subscription to 'onrefresh' with the 'subscribed' callback function.\n        // The function will be called when a subscription is published to\n        PubSub.subscribe('onrefresh', subscribed);\n\n        // ... further along in the code ...\n\n        // Publish to the 'onrefresh' subscription, in that any of those callback functions subscribed,\n        // will be called. No additional arguments have been provided\n        PubSub.publish('onrefresh');\n\n        // See examples/index.html for additional examples\n    \u003c/script\u003e\n```\n\n## ES2015\n\nThe module is written using ES2015, but is transpiled using [babel](https://babeljs.io) to ES5. The reason for using [babel](https://babeljs.io), is not all browsers currently support the ES2015 specification, though will likely change very soon. The transpiled files are located in the `dist` directory.\n\n## How to install\n\nIf you use bower, then just copy and paste the following command to the shell window. (**Note:** pubsub was already taken.)\n```shell\n    bower install pubsub-module\n```\n\nOtherwise just include `dist/PubSub_es5.min.js` somewhere in your document. The following module also supports AMD or Node.js module type loaders.\n\n## Documentation\n\nThe following documentation outlines in detail about using the following module.\n\n### Subscribe\n\nTo subscribe to a particular subscription or a list of subscriptions, pass either a string or an array of strings of the subscription(s). A callback function or an array of callback functions must be passed as the second argument, depending on the first argument type used. It's recommended that the callback function(s) be named and not anonymous functions (see `unsubscribe`).\nThe function will return either a 'handle' or an array of 'handles', depending on what was passed as the first argument i.e. an array of subscriptions will return an array of 'handles' and a subscription string will return a 'handle'.\n\n```javascript\n    // Using a string and callback function\n    PubSub.subscribe('subscription', callbackFunction);\n\n    // Using an array of strings and an array of callback functions. They must be the same length to work correctly\n    PubSub.subscribe([subscription1, subscription2, subscriptionN], [callbackFunction1, callbackFunction2, callbackFunctionN]);\n```\n\n### Unsubscribe\n\nTo unsubscribe from a particular subscription or a list of subscriptions, can be done by passing a string, an array of strings or a 'handle' returned by `subscribe`. If a string or an array of strings if passed, then pass either a callback function or array of callback functions respectively. The second argument is ignored if the first argument is passed a 'handle'.\nThe function returns true on successful unsubscription; otherwise, false.\n\n```javascript\n    // Using a string and callback function\n    PubSub.unsubscribe('subscription', callbackFunction);\n\n    // Using an array of strings and an array of callback functions. They must be the same length\n    PubSub.unsubscribe(['subscription1', 'subscription2', 'subscriptionN'], [callbackFunction1, callbackFunction2, callbackFunctionN);\n\n    // Using the 'handle' from subscribe()\n    const subHandle = PubSub.subscribe('subscription', callbackFunction);\n\n    // ... further along in the code ...\n\n    // Unsubscribe using the 'handle'\n    PubSub.unsubscribe(subHandle);\n```\n\n### Publish\n\nTo publish to a particular subscription or list of subscriptions, can be done by passing a string, an array of strings or a 'handle' returned by `subscribe`. The second argument is the argument(s) to pass to the callback functions that have registered with the subscription. The last argument passed to the callback function(s) will always be a comma delimited string (CSV), that outlines the subscription(s) that were published to (even if no arguments were passed).\nThe function returns the number of subscribers publish to.\n\n```javascript\n    // Publish to those who have subscribed to a subscription\n    PubSub.publish('subscription', arg1, arg2, argN ... [args are optional]);\n\n    // Using an array of strings\n    PubSub.publish(['subscription1', 'subscription2', 'subscriptionN'], arg1, arg2, argN ... [args are optional]);\n\n    // Using the 'handle' from subscribe()\n\n    // Callback function that will be invoked when the subscription is published to\n    function callBackFunction(arg1, arg2, argN, subscriptionsArg) {\n        // Display the subscriptions that were published to. Note: This is a delimited string using the comma (,) character\n        console.log(subscriptionsArg);\n    }\n\n    const subHandle = PubSub.subscribe('subscription', callbackFunction);\n\n    // ... further along in the code ...\n\n    // Publish using the 'handle'\n    PubSub.publish(subHandle, arg1, arg2, argN ... [args are optional]);\n```\n\n### Clear\n\nTo clear a particular subscription or list of subscriptions, can be done by passing a string, an array of strings or a 'handle' returned by `subscribe`. To clear all subscriptions, simply eliminate the first argument.\n\n```javascript\n    // Clear all subscriptions\n    PubSub.clear();\n\n    // Using a string\n    PubSub.clear('subscription');\n\n    // Using an array of strings\n    PubSub.clear(['subscription1', 'subscription2', 'subscriptionN']);\n\n    // Using the 'handle' from subscribe()\n    const subHandle = PubSub.subscribe('subscription', callbackFunction);\n\n    // ... further along in the code ...\n\n    // Clear using the 'handle'\n    PubSub.clear(subHandle);\n```\n\n### Interface\n\nThe module uses an underlying interface which is exposed via the `getInterface` function and therefore can be used adjacent to the global PubSub module without interference. The functions exposed are `subscribe`, `unsubscribe`, `publish`, `clear` and `getVersion`. See above for details about usage.\n\n```javascript\n    // Retrieve the module's interface\n    // 'subscribe', 'unsubscribe', 'publish' and 'clear'\n    const interface = PubSub.getInterface();\n\n    // Create a new instance of the interface\n    const myPubSub = new interface();\n\n    // Publish to those who have subscribed to a subscription (see above for more details)\n    // This does not publish to those subscribed to the global module\n    myPubSub.publish('subscription', arg1, arg2, argN ... [args are optional]);\n\n    // Publish to those who have subscribed to a subscription using the global module. This does not affect 'myPubSub'\n    PubSub.publish('subscription', arg1, arg2, argN ... [args are optional]);\n```\n\n### Version\n\nTo retrieve the version number of the module, use `getVersion`.\n\n```javascript\n    // Retrieve the version number of the module\n    const version = PubSub.getVersion();\n\n    // Display in the console\n    console.log(version);\n```\n\n## Contribute\n\nTo contribute to the project, you will first need to install [node](https://nodejs.org) globally on your system. Once installation has completed, change the working directory to the module's location and run the following command:\n\n```shell\n    npm install\n```\n\nAfter installation of the local modules, you're ready to start contributing to the project. Before you submit your PR, please don't forget to call `gulp`, which will run against [ESlint](http://eslint.org) for any errors, but will also minify the module and transpile using [babel](https://babeljs.io).\n\n##### Watch\nCall the following command to start 'watching' for any changes to the main JavaScript file(s). This will automatically invoke ESLint and Uglify.\n```shell\n    gulp watch\n```\n\n##### ESLint\nCall the following command to invoke ESLint and check that the changes meet the requirements set in .eslintrc.\n```shell\n    gulp eslint\n```\n\n##### Uglify\nCall the following command to invoke Uglify, which will minify the main JavaScript file(s) and output to a .min.js file respectively.\n```shell\n    gulp uglify\n```\n\n##### Build\nCall the following command to invoke babel, ESLint and Uglify.\n```shell\n    gulp\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoftwarespot%2Fpubsub","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsoftwarespot%2Fpubsub","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoftwarespot%2Fpubsub/lists"}