Dependency free publish/subscribe for JavaScript
Switch branches/tags
Pull request Compare This branch is 203 commits behind mroderick:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


PubSubJS is a dependency free library for doing publish/subscribe messaging in JavaScript.

In order to not have surprising behaviour where the execution chain generates more than one message, publication of messages with PubSub are done asyncronously (this also helps keep your code responsive, by dividing work into smaller chunks, allowing the event loop to do it's business).

If you're feeling adventurous, you can also use syncronous message publication (speedup in browsers), which can lead to some very confusing conditions, when one message triggers publication of another message in the same execution chain. Don't say I didn't warn you.


  • No dependencies
  • No modification of subscribers (jQuery custom events modify subscribers)
  • No use of DOM for exchanging messages
  • No reliance of running in a browser
  • Easy to understand (messages are async by default)
  • Small(ish)
  • Compatible! ES3 compatiable, should be able to run everywhere that can execute JavaScript
  • AMD / CommonJS module


// create a function to receive the message
var mySubscriber = function( msg, data ){
    console.log( msg, data );

// add the function to the list of subscribers to a particular message
// we're keeping the returned token, in order to be able to unsubscribe 
// from the message later on
var token = PubSub.subscribe( 'MY MESSAGE', mySubscriber );

// publish a message asyncronously
PubSub.publish( 'MY MESSAGE', 'hello world!' );

// publish a message syncronously, which is faster by orders of magnitude,
// but will get confusing when one message triggers new messages in the 
// same execution chain
PubSub.publishSync( 'MY MESSAGE', 'hello world!' );

// unsubscribe from further messages, using setTimeout to allow for easy 
// pasting of this code into an example :-)
    PubSub.unsubscribe( token );
}, 0);


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"

// BAD
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");


The tests are done using BusterJS and the excellent Sinon.JS.

Future of PubSubJS

  • Build script to create the following wrappers
    • jQuery plugin
    • Ender.js wrapper
  • Hierarchical addressing of topics, using either dots (some.hierarchy.of.topics) or slashes (/some/hierarchy/of/topics).
  • Better and more extensive usage examples

More about Publish/Subscribe


PubSubJS uses Semantic Versioning for predictable versioning.


  • v1.0.3
    • AMD / CommonJS module support (@fernandogmar)