Skip to content
A library for subscribing to custom events and firing them
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


A library for subscribing to custom events and publishing them.


npm install pubsubmanager

This library is basically used in scenarios where custom and mutiple eventing is needed.

It follows a publisher-subscribers model. An event if fired, multiple subscribers who're listening to that event receive that event.

Basic Scenario

For example, consider a scenario where on a particular action you need to perform multiple actions irrespective of eachother.

var PubSubManager = require('pubsubmanager');

var setProfile = function(sender, args) {
    //custom code

var setUserActive = function(sender, args) {
    //Custom code   

PubSubManager.subscribe('user.login', setProfile);
PubSubManager.subscribe('user.login', setUserActive);'user.login', this, {id: 'XCVDF1DD233', name : 'John'});


To publish an event you need to provide three arguments viz. eventname, sender and optional arguments
    "user.login", // event-name
    this, // a refernece to sender, this can be string or an object
    { name:'John' } // an object of arguments 


To subscribe an event you need to provide three arguments viz. eventname and callback

var subId = PubSubManager.subscibe(
    "user.login", // event-name
    callback      //callback


Whenever you subscribe to an event , you're returned with an subscriptionId. You can save this for unsubscribing to the event in future



This is an enhaced version of subscribe, which makes sure that before you subscribe to that event, all other subscriptions for that event are cleared.

PubSubManager.clearAndSubscribe("eventName", callback);


This method allows you to unsubscribe all other subscriptions for this event.


Other Scenarios

For example, consider you're writing a multipage app, and intend to reuse components/javascript files in these pages. There're cases where you may include not all but only some of these files. Thus, unintentionally giving way for exceptions, for undefined functions, which're defined in other script which was not included. This is the scenario where PubSubManager has its real purpose.


var PubSubManager = require('pubsubmanager');

var showMessage = function(sender, args) {
    //Your code

//Subscribe to message show event
PubSubManager.subscribe('', showMessage);


var PubSubManager = require('pubsubmanager');

var hideMessage = function(sender, args) {
    //Your code

//Subscribe to message hide event
PubSubManager.subscribe('message.hide', hideMessage);


var PubSubManager = require('pubsubmanager');
//Fire message show event'', this, { msg: 'This is a message' });

//Fire message hide event'message.hide', this, {});

In above example, consider if you include a.js and c.js, it won't throw an exception, as there is no subscriber for message.hide function.


MIT License

Something went wrong with that request. Please try again.