PubSubJS
PubSubJS is a dependency free publish/subscribe library for JavaScript.
PubSubJS has synchronisation decoupling, so messages are delivered asynchronously. This helps keep your program predictable as the originator of messages will not be blocked while consumers process messages.
For the adventurous, PubSubJS also supports synchronous message publication. This can give a speedup in some environments (browsers, not all), but can also lead to some very difficult to reason about programs, when one message triggers publication of another message in the same execution chain.
For benchmarks, see A Comparison of JS Publish/Subscribe Approaches
Key features
- Dependency free
- Synchronization decoupling
- ES3 compatible. PubSubJS should be able to run everywhere that can execute JavaScript. Browsers, servers, ebook readers, old phones, game consoles.
- AMD / CommonJS module support
- No modification of subscribers (jQuery custom events modify subscribers)
- Easy to understand and use (thanks to synchronization decoupling)
- Small(ish), less than 1kb minified and gzipped
Getting PubSubJS
There are several ways of getting PubSubJS
Examples
Basic example
var mySubscriber = function( msg, data ){
console.log( msg, data );
};
var token = PubSub.subscribe( 'MY MESSAGE', mySubscriber );
PubSub.publish( 'MY MESSAGE', 'hello world!' );
PubSub.publishSync( 'MY MESSAGE', 'hello world!' );
Cancel specific subscripiton
var mySubscriber = function( msg, data ){
console.log( msg, data );
};
var token = PubSub.subscribe( 'MY MESSAGE', mySubscriber );
PubSub.unsubscribe( token );
Cancel all subscriptions for a function
var mySubscriber = function( msg, data ){
console.log( msg, data );
};
var token = PubSub.subscribe( 'MY MESSAGE', mySubscriber );
PubSub.unsubscribe( mySubscriber );
Hierarchical addressing
var myToplevelSubscriber = function( msg, data ){
console.log( 'top level: ', msg, data );
}
PubSub.subscribe( 'car', myToplevelSubscriber );
var mySpecificSubscriber = function( msg, data ){
console.log('specific: ', msg, data );
}
PubSub.subscribe( 'car.drive', mySpecificSubscriber );
PubSub.publish( 'car.purchase', { name : 'my new car' } );
PubSub.publish( 'car.drive', { speed : '14' } );
PubSub.publish( 'car.sell', { newOwner : 'someone else' } );
Tips
Use "constants" for topics and not string literals. PubSubJS uses strings as topics, and will happily try to deliver
your messages with ANY topic. So, save yourself from frustrating debugging by letting the JavaScript engine complain
when you make typos.
Example of use of "constants"
PubSub.subscribe("hello", function( msg, data ){
console.log( data )
});
PubSub.publish("helo", "world");
var MY_TOPIC = "hello";
PubSub.subscribe(MY_TOPIC, function( msg, data ){
console.log( data )
});
PubSub.publish(MY_TOPIC, "world");
Immediate Exceptions for stack traces in developer tools
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.
PubSub.immediateExceptions = true;
Plugin for jQuery
By default PubSubJS can be used in any browser or CommonJS environment, including node. Additionally, PubSubJS can be built specifically for jQuery.
$ rake jquery
Produces jquery.pubsub.js
Use with jQuery
var topic = 'greeting',
data = 'world'
subscriber = function sayHello( data ){
console.log( 'hello ' + data );
};
var token = $.pubsub('subscribe', topic, subscriber );
$.pubsub('unsubscribe', token)
$.pubsub('unsubscribe', subscriber);
$.pubsub('publish', topic, data);
$.pubsub('publishSync', topic, data);
In the jQuery build, the global PubSub
global is still available, so you can mix and match both Pubsub
and $.pubsub
as needed.
There is also an article about Using PubSubJS with jQuery
Contributing to PubSubJS
Please see CONTRIBUTING.md
Future of PubSubJS
- Build script to create the following wrappers
- Better and more extensive usage examples
More about Publish/Subscribe
Versioning
PubSubJS uses Semantic Versioning for predictable versioning.
Changelog
Please see https://github.com/mroderick/PubSubJS/releases
License
MIT: http://mrgnrdrck.mit-license.org
Alternatives
These are a few alternative projects that also implement topic based publish subscribe in JavaScript.