Dependency free publish/subscribe library
PubSubJS has synchronisation decoupling, so topics are published asynchronously. This helps keep your program predictable as the originator of topics will not be blocked while consumers process them.
For the adventurous, PubSubJS also supports synchronous topic publication. This can give a speedup in some environments (browsers, not all), but can also lead to some very difficult to reason about programs, where one topic triggers publication of another topic in the same execution chain.
For benchmarks, see A Comparison of JS Publish/Subscribe Approaches
PubSubJS is designed to be used within a single process, and is not a good candidate for multi-process applications (like Node.js – Cluster with many sub-processes). If your Node.js app is a single process app, you're good. If it is (or is going to be) a multi-process app, you're probably better off using redis Pub/Sub or similar
There are several ways of getting PubSubJS
npm install pubsub-js)
bower install pubsub-js)
// create a function to subscribe to topicsvarconsole.log msg data ;;// add the function to the list of subscribers for a particular topic// we're keeping the returned token, in order to be able to unsubscribe// from the topic later onvar token = PubSubsubscribe 'MY TOPIC' mySubscriber ;// publish a topic asyncronouslyPubSubpublish 'MY TOPIC' 'hello world!' ;// publish a topic syncronously, which is faster in some environments,// but will get confusing when one topic triggers new topics in the// same execution chain// USE WITH CAUTION, HERE BE DRAGONS!!!PubSubpublishSync 'MY TOPIC' 'hello world!' ;
// create a function to receive the topicvarconsole.log msg data ;;// add the function to the list of subscribers to a particular topic// we're keeping the returned token, in order to be able to unsubscribe// from the topic later onvar token = PubSubsubscribe 'MY TOPIC' mySubscriber ;// unsubscribe this subscriber from this topicPubSubunsubscribe token ;
// create a function to receive the topicvarconsole.log msg data ;;// unsubscribe mySubscriber from ALL topicsPubSubunsubscribe mySubscriber ;
PubSubsubscribe'a' myFunc1;PubSubsubscribe'a.b' myFunc2;PubSubsubscribe'a.b.c' myFunc3;PubSubunsubscribe'a.b';// no further notications for 'a.b' and 'a.b.c' topics// notifications for 'a' will still get published
PubSubclearAllSubscriptions;// all subscriptions are removed
// create a subscriber to receive all topics from a hierarchy of topicsvarconsole.log 'top level: ' msg data ;// subscribe to all topics in the 'car' hierarchyPubSubsubscribe 'car' myToplevelSubscriber ;// create a subscriber to receive only leaf topic from hierarchy op topicsvarconsole.log'specific: ' msg data ;// subscribe only to 'car.drive' topicsPubSubsubscribe 'car.drive' mySpecificSubscriber ;// Publish some topicsPubSubpublish 'car.purchase' name : 'my new car' ;PubSubpublish 'car.drive' speed : '14' ;PubSubpublish 'car.sell' newOwner : 'someone else' ;// In this scenario, myToplevelSubscriber will be called for all// topics, three times in total// But, mySpecificSubscriber will only be called once, as it only// subscribes to the 'car.drive' topic
// BADPubSubsubscribe"hello"console.log data;PubSubpublish"helo" "world";// BETTERvar MY_TOPIC = "hello";PubSubsubscribeMY_TOPICconsole.log data;PubSubpublishMY_TOPIC "world";
As of versions 1.3.2, you can force immediate exceptions (instead of delayed execeptions), which has the benefit of maintaining the stack trace when viewed in dev tools.
This should be considered a development only option, as PubSubJS was designed to try to deliver your topics to all subscribers, even when some fail.
Setting immediate exceptions in development is easy, just tell PubSubJS about it after it's been loaded.
PubSubimmediateExceptions = true;
By default PubSubJS can be used in any browser or CommonJS environment, including node. Additionally, PubSubJS can be built specifically for jQuery using Rake.
$ rake jquery
or using Grunt
$ grunt jquery
var topic = 'greeting'data = 'world'subscriber =console.log 'hello ' + data ;;// add a subscriptionvar token = $pubsub'subscribe' topic subscriber ;// unsubscribing$pubsub'unsubscribe' token; // remove a specific subscription$pubsub'unsubscribe' subscriber; // remove all subscriptions for subscriber// publishing a topic$pubsub'publish' topic data;// publishing topic syncronously$pubsub'publishSync' topic data;
In the jQuery build, the global
PubSub global is still available, so you can mix and match both
$.pubsub as needed.
There is also an article about Using PubSubJS with jQuery
Please see CONTRIBUTING.md
PubSubJS uses Semantic Versioning for predictable versioning.