Loosely Coupled JavaScript Using PubSub

I recently watched a very interesting video by Rebecca Murphy that discussed using the pubsub architecture to create loosely coupled JavaScript. The concept is to develop your JavaScript objects and allow the communication between the objects to occur via events that are managed by a publisher / subscriber (pubsub) service. The diagram below illustrates a pubsub service that exposes three methods: publish, subscribe, and unsubscribe. In this case, two ‘panels’ are subscribing to the service.

image A data source in the system then publishes data to the pubsub service. The pubsub service forwards this data to the appropriate subscribers. This is illustrated below.

imageThe pubsub service allows subscriptions to specific ‘events’. Much like you can subscribe to specific channels with your cable TV provider.

Let’s take a look at a specific example. Imagine we have a web page. On that we page is a form that allows the user to enter their name, lucky number, and select a favorite color.

imageIn addition to this form we have a number of other panels that present the data in a specialized way. For example we could have a panel that presents the names of all registered users. Another panel could present data about only the most recent users. While another displays a list of unique lucky numbers. Yet another presents color swatches of all the favorite colors. The development of these panels could be quite complex. Each will probably have its own HTML, CSS, and JavaScript.


By using the pubsub service to couple these panels to the data source, each can be developed independently of the others. The development process feels a bit like using an IOC container. Develop each component and then bind them together. Here is a link to a demo page.



The markup for this example is shown below:

<div id="page">
    <div id="form">
            <label for="first">First Name</label>
            <input type="text" id="first" />
            <label for="last">Last Name</label>
            <input type="text" id="last" />
            <label for="number">Lucky Number</label>
            <input type="text" id="number" />
            <label for="color">Favorite Color</label>
            <input type="text" id="color" name="color" value="#006699" />
            <div id="colorpicker"></div>
            <button id="add">Add</button>

    <div id="panel-wrap">
        <div id="names" class="panel"></div>
        <div id="lastuser" class="panel"></div>
        <div id="numbers" class="panel"></div>
        <div id="colors" class="panel"></div>


There are two main sections. The top defines the form (in a literal sense not the html element) that is used as the data source. The bottom creates placeholders for the panels.


I am using a slightly modified version of a pubsub plug-in created by Peter Higgins. The only modification that I did was to add a try catch block around the publish method. Without that modification any one subscriber can prevent later subscribers from receiving the event by having ‘exceptional code’ (in a bad way). For completeness here is the final code (without the comments):

    var cache = {};

    d.publish = function(/* String */topic, /* Array? */args){
            d.each(cache[topic], function(){
                this.apply(d, args || []);
        } catch (err) {
            // handle this error

    d.subscribe = function(/* String */topic, /* Function */callback){
            cache[topic] = [];
        return [topic, callback]; // Array

    d.unsubscribe = function(/* Array */handle){
        var t = handle[0];
        cache[t] && d.each(cache[t], function(idx){
            if(this == handle[1]){
                cache[t].splice(idx, 1);



The main page uses this service to bind publishers and subscribers to each other. The relevant JavaScript is shown below:

// Initialize the panels
//    1st param: the container element
//    2nd param: the event to subscribe
var userPanel = new UserPanel(      $('#names'),    'new/user');
var lastPanel = new LastUser(       $('#lastuser'), 'new/all');
var numberPanel = new LuckyNumbers( $('#numbers'),  'new/number');
var colorPanel = new FavColors(     $('#colors'),   'new/color');

// Bind to the click event

    // Get the values from the form
    var first = $('#first').val();
    var last = $('#last').val();
    var color = $('#color').val();
    var number = $('#number').val();
        alert("woah...that's not a number");

    // Publish the new data
    $.publish('new/user',   [first,last]);
    $.publish('new/number', [number]);
    $.publish('new/color',  [color]);
    $.publish('new/all',    [first,last,number,color]);

First this code creates and initializes four panel objects. Each panel takes a container element and an event in the constructor. Each panel then subscribes to the appropriate event and renders itself. The code above binds to the ‘click’ event of the form that we created earlier. At the bottom of that binding are the publish events supported by the form. The data is sliced up and made available via a number of events that expose only that slice.

Each panel is contained in its own JavaScript file. They all follow the same pattern. The following code shows the ‘UserPanel’ object. View source the demo for the other panels.

function UserPanel(parent, event){
    var _parent = parent;
    var _html = '';
    var _title = 'Registered Users';
    var _tmpl = '<li>{{name}}</li>';

    var _renderUserPanel = function(name){
            var elem = _tmpl.replace('{{name}}', name);
            _html = _html + elem;

        var result = '<h3>' + _title + '</h3>';
            result += '<ul>' + _html + '</ul>';
        } else {
            result += '<ul><li>no users</li></ul>';

    var _subscribe = function(event){
        $.subscribe(event, function(first, last){
            var name = first + ' ' + last;

    // render the initial markup


There is a definite trade off with this architecture. On one hand there is a bit more code. On the other hand the architecture is more decoupled. I struggled a bit to come up with a meaningful example that tips the balance in favor of decoupled architecture. With simple pubsub examples, the added code appears as too much cost for the added benefit. I hope this example has found the right balance. If you know of a better example, please provide links in the comments.

  1. Sitebase
  2. mithun_daa
    • rcravens
  3. Garth
  4. Ted
  5. gold exploit diablo 3

Leave a Reply

Your email address will not be published. Required fields are marked *