New Beginnings

On March 18, 2013, in Job, by Jim Cowart

I have been incredibly fortunate to have met so many wonderful people – and forged many good friendships – in my career up to this point. As I reflect upon the caliber of those I’ve been privileged to work with, it’s very humbling and I’m deeply grateful.  For a little over a year, I’ve worked with one such group of people at appendTo. I’ve learned, laughed, worked, created – you name it – harder than I would have imagined. My time at appendTo drew to a close last week, and I sincerely wish each and every person there future success and continued passion in doing what we love to do.

So, in the spirit of continuing to be honored to work with such awesome people, I’m very pleased to announce that I am joining Telerik as a developer advocate! I will be working on the web team, focusing on Icenium, alongside the likes of Burke Holland, Derick Bailey, Brandon Satrom & Todd Anglin (to name a few). I’ve been impressed with Telerik for some time now – and am especially excited that their approach to developer advocacy focuses on teaching and elevating developer knowledge, supporting the developer community and giving back in general. I’m very excited about products like Icenium and KendoUI (and more to come!), and look forward to reaching out to other developers at community events to talk about the web in general, JavaScript OSS (a huge passion of mine) and where/how Telerik tools could be a fit for the challenges they face.

That doesn’t at all mean that my blog here on Fresh Brewed Code will become a long Telerik ad. If anything, I hope to blog more frequently here, as well as my long-neglected personal blog. Of course, I’ll soon be posting to the Icenium blog as well. I also plan to continue with the many OSS projects I’ve had on my plate – and squeeze in time for some new ones I’ve been working on.

It’s an amazing time to be a web developer – and I’m thrilled to be pushing the web forward with such a great company and team!

Tagged with:  

Cross Frame Messaging with postal.xframe

On February 26, 2013, in JavaScript, by Jim Cowart

Writing open source software provides some great moments of being the giddy kid in the candy store. Of course, too much candy is never a good idea, so hopefully I’m not over-doing it with chocolate-covered cross-frame messaging.

Wait – chocolate-covered what?!

Cross-frame messaging.  ”But why would I need to do this?” What – cover something in *chocolate*? Are you serious? Ohhh, you mean “why would I need to use cross-frame messaging?” Fantastic question. Perhaps you want your site to be “mashable” (usable in other sites) or are otherwise writing “widgets” that will be embedded – via iframe – into another site.  Maybe you want to move some CPU-bound problems to a hidden iframe – or a web worker – to prevent it from bogging down your main window as users interact with it. Cross-frame messaging comes into play when you need to communicate between the windows (and/or workers). The good news is, the tools are available (in most cases) to make this happen. The bad news is these tools are of the “lowest common denominator” variety – so generalized that you almost always need a higher level abstraction to prevent infrastructure concerns from running rampant in your code. I can’t promise chocolate (that’s up to you). However, I can hopefully provide some ‘sugar’ for a situation that quickly turns bitter.

The Window Over There

Cross-frame messaging is an interesting beast to tame. Most modern browsers support postMessage, which allows you to send messages between windows. Here’s a basic snippet of how it works:

// Somewhere in our iframe, for example:
window.addEventListener("message", function(e) {
    if (e.origin !== "http://www.freshbrewedcodes.com:3080") {
        return;
    }
    // yeah, cause in the real world we just want
    // to console.log messages. AMIRITE?!
    console.log(JSON.stringify(e.data, null, 4));
    // Curious about the extra stringify args?
    // See http://bit.ly/VlwCQb
}, false);

// Somewhere in our parent window, for example
var target = document.getElementById("zeeiframe");
target.contentWindow.postMessage({
    food           : "bacon",
    description : "Yummy"
}, "http://www.freshbrewedcodes.com:3080");

/* iframe would print:
{
    "food": "bacon",
    "description": "Yummy"
}
*/

MDN is a great resource for further information on postMessage if you want to read more.  You can see from the above example that we listen for messages via “addEventListener” (or “attachEvent” if you’re in IE 8). You can infer from the “message” event callback that it receives an argument that contains at least an “origin” and “data” property (oh, it has much more). Assuming the sender was allowed to send a message to you, you get to handle checking from that point on, usually by verifying that you actually do want to process a message from the message’s origin.  When sending a message, you provide the message data and an origin (I advise against using the infamous “*” origin option in most circumstances). If you’ve had to work in this space before, this is all old news, of course. The DOM gods smiled upon us and provided a ‘pipeline’ between windows, right?

Really, though, it’s the rawest of pipelines. Every message comes across as a “message” event, and if multiple components are utilizing postMessage, your handlers will need to filter out and ignore irrelevant messages. The above example just accounts for filtering by origin. Beyond that, it’s up to you to add additional metadata to the payload by which to filter messages. So – with even a moderate level of complexity, the need for some sort of abstraction on top of postMessage quickly becomes apparent – something to help unpack and route messages. Not only that, but if you want it to work in IE 8 or 9, you’ll have to eagerly serialize your payloads to JSON and parse them on the receiving side, since they only support sending strings as the message payload. Of course, you don’t have to wrap postMessage in an API – you could bake all that infrastructure code right into your application logic (really, please don’t). Wouldn’t it be nice if the components communicating could operate completely ignorant of the barrier? What if they could do so using an API you might already be using to de-couple your components? This is actually a goal I had in mind early on when I wrote postal.js.

Over the last few months I’ve tried solving this problem several different ways. After a couple of failed attempts, and a couple of mediocre successes – all of which I tossed – I believe I’ve finally settled on an approach that I may not want to burn to the ground next week and dance like an elf in the cinders and ash. The first step in this approach was postal.federation.

Step One: postal.federation

The postal.federation add-on to postal.js doesn’t do anything on its own, other than add the core functionality necessary to federate postal.js instances. This “core” functionality includes top level API calls, and the ability to whitelist or blacklist both inbound and outbound messages, but without a “transport” plugin, there’s no “wire” over which to communicate with remote postal instances. The goal is for postal.federation to carry the concerns that would be common to any transport plugin, minus the actual implementation details on how to communicate across that transport.

Step Two: postal.xframe

The first transport plugin (at least, the first one that works with this architecture) is postal.xframe. postal.xframe provides the behavior necessary for postal instances in a window to communicate with postal instances in another accessible window or in a dedicated web worker. In effect, it wraps the postMessage call (on both ends), using it as a bridge to ship selected postal envelopes to and from remote windows/workers. What’s interesting about this is that if you’re already decoupling pieces of your application using postal, you can actually move some of the components into an iframe or worker, for example, and as long as postal.xframe is present (and postal.federation’s filters are allowing the messages to pass), they will continue to operate as they always have.

“Yeah, but really, Jim. How often do you want to move components into an iframe just to show they can continue to work as expected, with no changes?”

Exactly – this isn’t a common use-case. But if you’re one of the many, like myself, that’s had to solve the pain of cross-frame communication, you know how nice it would be to have a consistent (not to mention flexible and powerful) API you can use to drop into any cross-frame situation. So, what does it look like to use postal.xframe?

Sample App

I’ve created an xframe sample app and – as many sample apps go – it’s silly and contrived (even makes use of much-loathed “word clouds”, sorry John Sheehan). Here’s the gist:

  • The main page (index.html) contains a search box (top right), which defaults to “JavaScript”. Clicking “initialize” will kick off a server-side search utility against Twitter’s public HTTP API for that term. (Search results will be rendered in 3 iframes, explained below)
  • When the parent page (index.html) loads, it loads up 5 iframes and federates with 4 of them via postal.xframe.
    • 1 frame contains the README from the repo (shown initially, by default). It disappears as soon as you initialize a search. (This iframe isn’t federated via postal.)
    • The “comm” iframe is a “communication iframe” – it contains a socket connection to the server, and is not visible
    • The “stream” iframe contains assets necessary to render a twitter timeline based on the search results
    • The “language” iframe contains assets necessary to render a raphael.js-based pie chart, showing a percentage breakdown by language in the tweets returned from the search
    • The “hashtag” iframe contains assets necessary to render a canvas-based tag cloud of all the hashtags represented in the search results
  • As the server process search results from twitter, messages are batched and sent to the comm iframe via socket.io. The comm iframe publishes these results via postal, which results in them being passed up into the parent iframe and onto the other federated iframes.
  • There is an alternate example page – worker.html – which replaces the “comm” iframe with a worker – everything continues to function as expected, just one less iframe. Caveat emptor: postal.xframe’s API still needs to be evolved – the worker support especially is perhaps one notch above “bare bones” – but it’s a start. Of course, to run the worker-enabled version of the demo, you’ll need a browser that supports workers.

I won’t cover every detail, so let’s look at the highlights:

// Taken from the top of the parent-main.js module
postal.instanceId("parent");

postal.fedx.addFilter([
  { channel: 'search',  topic: '#', direction: 'both' },
  { channel: 'ctrl', topic: '#', direction: 'out'  },
  { channel: 'language-stats', topic: '#', direction: 'both' },
  { channel: 'hashtag-stats',  topic: '#', direction: 'both' },
  { channel: 'user-stats',  topic: '#', direction: 'both' }
]);

postal.fedx.signalReady();

The above snippet shows how the parent window configures itself to work via xframe.

  • instanceId() – Every instance of postal must have a way to be uniquely identified if it federates with other instances. The postal.federation core bits add a postal.instanceId() method to provide this. In our case, instead of a GUID or otherwise random identifier, we’re cheating and calling this postal instance “parent”, since it lives in the parent window.
  • filters – By default, postal.federation starts in “whitelist” mode, which means ONLY messages matching any added filters will sent to/from remote instances of postal via federated connections. Unless we add filters, or switch the mode to “blacklist”, no messages will leave the local window. Filters can affect inbound messages, outbound messages or both (via “in”, “out” and “both”). The channel & topic values are passed to the bindings resolver (same one used to route messages to subscribers), so it’s possible to use topic wildcards here.
  • signalReady() – This is how the local instance of postal reaches out to see if any other instances can federate. Each transport plugin would provide it’s own implementation of *how* to signalReady, and since we’re using postal.xframe in this scenario, it will (by default) find all the iframes in the document and post a message to each that will start the postal federation handshake, if postal is on the other end as well.

So – that’s the parent window, what about one of the “widget” iframes? Let’s look at an example from the “language stats” pie chart widget:

// taken from the languageStats-main.js module
postal.instanceId("language-stats");
postal.fedx.addFilter([
  { channel: 'language-stats', topic: '#', direction: 'in' },
  { channel: 'search', topic: 'started', direction: 'in' }
]);

// and later:
postal.fedx.signalReady();

Each “iframe” that joins in the federation has similar set-up code along these lines. Aside from that, however, the rest of the code is written with zero knowledge nor baked-in assumption that messages might originate at a remote location.

So What About Cross Domain Security?

I’m glad you asked. In order for windows served from different origins to communicate, the burden is on you to ensure that the proper headers are in the response(s) and that you’re using a client that supports CORS. (See the further reading at the end of the post.) Assuming you have all that in place, here’s an example of configuring postal.xframe to set allowed origins from/to which you’ll allow messages to be sent:

// "allowedOrigins"- origins you're ok interacting with
// "defaultOriginUrl" the origin to be passed to postMessage
// when postal.xframe transmits messages to other windows
postal.fedx.transports.xframe.configure({
    allowedOrigins : [
        "http://some.host.com",
        "http://another.com"
    ],
    defaultOriginUrl : "http://some.host.com"
});

So, About that “Overkill”

This example is somewhat over the top. After all, it’s not everyday that you’ll have multiple widgets that could open their own socket connections back to the same source. However, I have run into this very situation – and opening multiple socket connections back to a single source is a recipe for the devops-pocalypse™. In that case, postal.xframe made it possible for the widgets to share the same connection. In addition, I’ve run into simpler situations where the page hosted a form widget (in an iframe), needed some sort of clean way to exchange data between parent and child windows. The setup overhead in both circumstances is the same – and given what’s being done under the hood, I consider it a pretty light configuration overhead.

You’ll definitely find more everyday use-cases for postal.js itself vs the federation add-ons. But if you need them, they’re easy to get, setup and use.  In the meantime, pull down the sample app and have some fun….

Also – feel free to watch the high level walk-through of the xframe demo app (forgive the grainy quality):

Futher Reading

window.postMessage at MDN

Enable CORS

Cross Origin Resource Sharing on Wikipedia

MDN – CORS

MDN – Worker

postal wiki

postal.federation core add-on (github)

postal.xframe transport plugin (github)

Want a different option than postal.xframe? Check out Easy XDM.

Tagged with:  

Getting Into Context Binds

On February 12, 2013, in JavaScript, by Jim Cowart

I was source-diving a Backbone.js application recently and noticed the liberal use of _.bindAll(this) calls throughout the code base. Then, in another jQuery-heavy application, I noticed frequent use of the $.proxy() call. In the past, I have certainly been guilty of the former – you know the “What the heck is ‘this’, this time? Nuke it from orbit and just bind all the methods!” But seeing it so often in others’ code made me wonder – are they aware of what’s happening? You probably are, but let’s look anyway:

var Person = function( name ) {
    this.name = name;
};
Person.prototype.greet = function() {
    return "Oh, hai " + this.name;
};
Person.prototype.sayBye = function() {
    return "Get lost, " + this.name;
};

var me = new Person("Jimbabwe");

Inspecting the above instance will show us something similar to this:

Inspecting the person instance

Notice that the “greet” and “sayBye” methods appear on the prototype of “Person”.

Now let’s pretend that we’ve gotten terribly frustrated because our “greet” method is being invoked asynchronously (we assigned it as the click event handler on a fictional ‘greet’ button), and we’re consistently seeing “Oh, hai undefined”. In our frustration, we “brute force” the context to be the proper “this” by doing, well, this:

// assuming underscore is present....
var Person = function( name ) {
    this.name = name;
    _.bindAll( this );
    // FYI, an alternative to the above _.bindAll could be:
    // _.bindAll( this, "greet", "sayBye");
    // args following the context specify method names to bind
};
Person.prototype.greet = function() {
    return "Oh, hai " + this.name;
};
Person.prototype.sayBye = function() {
    return "Get lost, " + this.name;
};

var me = new Person("Jimbabwe");

Now, when we inspect the “me” instance, we’ll see something like this:

inspecting the bound instance

That’s right – using .bind, .bindAll, $.proxy or even plain ES5 Function.prototype.bind() to bind the “this” context results in an instance-level method. This should be obvious, right? Surprisingly enough, many of the people I’ve talked to haven’t stopped to think about this. That sound you hear is the mini-Crockford-angel (or devil, perhaps) on your right shoulder, reminding you that by creating instance-level methods, you’ve erased the gain of having methods on the prototype. If the method stays on the prototype, it’s created once and then shared by future “Person” instances. There is a very real memory and CPU performance loss in wastefully assigning instance members when you don’t need to.  Consider this:

Performance Comparison

All three tests use the same prototype – where “Normal” has no context binding, and the others use underscore and jQuery, respectively, to bind the context of the prototype methods in the constructor functions.

Ok – so we’ve established that explicitly binding the context of a prototype method creates an instance level method, as well as the fact that doing so unnecessarily can result in a serious performance drop – but are there any other potential gotchas? I can think of at least one – stealing a bound method:

var Person = function( name ) {
    this.name = name;
    _.bindAll( this );
};
Person.prototype.greet = function() {
    return "Oh, hai " + this.name;
};
Person.prototype.sayBye = function() {
    return "Get lost, " + this.name;
};

var me = new Person("Jimbabwe");

var obj = { name: "Eduardo" };

me.greet.call(obj);
// "Oh, hai Jimbabwe"

I’m going to go ahead and concede that I have no idea why someone, in their right mind, would need to steal the “greet” method from the “me” instance and call it on “obj” – as long as you concede to me that there’s no end to the unpredictability of developers doing crazy nonsensical things after you’ve bequeathed a pristine codebase to them. But the point still stands – it’s important to realize that once you’ve bound the method, it’s bound. The only way around unexpected side effects in the above example would be to call “Person.prototype.greet.call(obj);” – probably not a bad practice in general, should you need to steal methods at some point.

Hold On a Second, Let’s Back Up

This begs the question – why did the developer feel the need to bind the method to begin with? “This” wasn’t what they expected or needed, of course! But why does that happen? When “greet” is invoked on the “me” instance, it’s invoked as a method. The context – “this” – points to the instance. However – what happens when we take that method and assign it as the callback to be invoked when an event occurs? The event will invoke the callback asynchronously – at some point when (in this case) we click the button:

$("#greeter").on("click", me.greet);

When the event occurs, the element will own the context as the callback is invoked. When the function attempts to access “this.name”, it will get an undefined value. Avoiding this error is obviously important, but does it have to be solved by binding all the prototype methods to the instance? No! You can bind at the point of subscribing to the event:

// using $.proxy
$("#greeter").on("click", $.proxy(me.greet, me));

// using _.bind
$("#greeter").on("click", _.bind(me.greet, me));

// using ES5 (or a shim)
$("#greeter").on("click", me.greet.bind(me));

Some eventing libraries provide explicit facilities to let you specify the context, without having to actually pass a bound function. (In fact, Backbone binds the context of your DOM event handlers automatically to the view for you as it wires them up.) Under the hood, many (but not all) of those libraries are typically using call/apply to pass the context to the function as it is invoked.

// postal subscription on a channel
var sub = channel.subscribe("my.topic", callback)
                 .withContext(something);

// Backbone Events - can pass context as optional 3rd arg
someModel.on("change:firstName", doStuff, this);

It’s a safe assumption that not every method on your instances will be invoked asynchronously, as a handler to an event. So – binding the context on an as-needed basis (as you register event callbacks) will have a much more performance-friendly CPU & memory footprint.

Ok, so:

  • Binding prototype methods creates instance level methods? Check.
  • Instance level methods are created each time the instance is, and result in decreased performance? Check.
  • Stealing bound methods can violate the principle of least surprise? Check.
  • It’s better to only bind the context in situations that actually will result in changed context? Check.

Is there anything else we should be aware of? Yes:

// using _.bind on me.greet
$("#greeter").on("click", _.bind(me.greet, me));

// This will not remove the event handler
$("#greeter").off("click", me.greet);

The above example is stating the obvious, I know. But it’s a good reminder that binding the context of a function produces a new function. If you plan to remove the event subscription, you’ll need to either hang on to a reference to the bound function to remove it specifically, use a namespace to separate it from other handlers, or be willing to clear all the events, etc. Also – it’s worth noting that jQuery goes a bit further with $.proxy, so that it’s possible to unbind a bound handler by passing the original function reference as opposed to the bound one. Regardless, though, jQuery’s own documentation still recommends that you use event namespaces.

So – armed with this – may your binding be bind-free….

Tagged with:  

Client-side Messaging Essentials

On February 7, 2013, in JavaScript, by Jim Cowart

Wow – dangerous title, right? Our grasp of what is essential certainly changes alongside our understanding of the problems which we are trying to solve. So I won’t lie to you – what I thought was essential a year ago was woefully incomplete, as I’m sure what I’m about to write will seem to me six months down the road. So – this post is a snapshot in time of a few of the things I’ve found to be critical for successfully applying client-side messaging patterns in JavaScript.

1.) Understanding the Difference Between Mediator and Observer

One Does Not Simply "Pub/Sub"

Most people throw around the term “pub/sub” to describe any kind of eventing/messaging – but I think this term fails to adequately communicate the abstraction. Yes, ultimately, something is subscribing to what something else is publishing. But the level at which publishers and subscribers are encapsulated has the power to make a good pattern turn to the dark side. So, what’s the difference?

Observer

The observer pattern involves a subject being observed by one or more observers. The subject typically keeps track of the observers, usually by storing a list of callback methods registered by the observer(s) as they subscribe to be notified. Observe: (oh, puns, how I love them)

var observer = {
    listen : function() {
        console.log("Yay for more cliché examples...");
    }
};
var elem = document.getElementById("cliche");
elem.addEventListener("click", observer.listen);

A couple of things to note:

  • we had to get a direct reference to the subject
  • the subject has to maintain some kind of internal state, keeping track of the observers’ callbacks.
  • while our listener isn’t utilizing any arguments passed back from the subject, there could, in theory, be 0-n* arguments (more on how that gets to be fun later)

* where n isn’t truly infinite, but for the purposes of discussion, it’s a limit we should never reach

Mediator

The mediator pattern introduces a “third party” between a subject and an observer – effectively decoupling the two and encapsulating how they communicate. A mediator’s API may be as simple as “publish”, “subscribe” and “unsubscribe” calls, or a domain-specific implementation might be given to hide those kinds of methods behind something more semantically meaningful.  Most server-side implementations I’ve worked with were more domain-specific than simple, but there’s no rule against a generic mediator! It’s not uncommon to think of a generic mediator as a type of message broker. In either case, the outcome is the same – the subject and observer no longer know directly about one another:

// It's fun to be naive!
var mediator = {
    _subs: {},
    // a real subscribe would at least check to make sure the
    // same callback instance wasn't registered 2x.
    // Sheesh, where did they find this guy?!
    subscribe: function(topic, callback) {
        this._subs[topic] = this._subs[topic] || [];
        this._subs[topic].push(callback);
    },
    // lolwut? No ability to pass function context? :-) 
    publish : function(topic, data) {
        var subs = this._subs[topic] || [];
        subs.forEach(function(cb) {
            cb(data);
        });
    }
}
var FatherTime = function(med) { this.mediator = med; };
FatherTime.prototype.wakeyWakey = function() {
    this.mediator.publish("alarm.clock", {
        time: "06:00 AM",
        canSnooze: "heck-no-get-up-lazy-bum"
    });
}
var Developer = function(mediator) {
    this.mediator = mediator;
    this.mediator.subscribe("alarm.clock", this.pleaseGodNo);
};
Developer.prototype.pleaseGodNo = function(data) {
    alert("ZOMG, it's " + data.time + ". Please just make it stop.");
}
var fatherTime = new FatherTime(mediator);
var developer = new Developer(mediator);
fatherTime.wakeyWakey();

Aside from the rather naive mediator implementation, you get the idea. The subject is no longer responsible for maintaining a list of subscribers, and both “fatherTime” and the “developer” instances never actually know about each other. They only share a message – which is an important contract, as we’ll see later. “Great, Jim. It’s still all pub/sub to me, so what’s the big deal? Does it really make a difference as to which approach I take?” Read on, dear straw man, read on.

2.) Know When to Use Mediator or Observer

The tl;dr – my own guiding principle when I’m faced with this: use observer “locally”, inside a component, mediator “remotely” between components. No matter what, be ready to use both in tandem.

But that’s a horrible tl;dr. It’s like trying to sum up months of nuanced experience in the trenches in less than 140 characters. The reality of answering this question isn’t always so neat. So here’s the longer version of the internal conversation:

  • Should the observer have a direct reference to the subject for reasons other than just because it’s interested in data emitted? For example – a Backbone.View has every reason to have a direct reference to its model. The very nature of the relationship means that the view might not only be interested in rendering when a model change event occurs, but it may directly invoke fetch on the model in an event handler. If the answer to this question is yes, then observer makes sense.
  • However – if the nature of the observer-subject relationship is solely about data, and a mediated pub/sub approach would work, I’ll go with that. Communication between two Backbone.View instances, or between two models (to pick on Backbone again) are examples of where this might be applicable. A view controlling a navigation menu might publish information about the current state of the app in which a view controlling a breadcrumb widget is interested (say, the current nav hierarchy). The breadcrumb widget doesn’t need a direct reference to the nav view, it just wants information that the nav view happens to provide. The key here is that the nav view might not be the ONLY one providing that information, and that both views could continue on without the other present. Mediated pub/sub is ideal here – and lends itself very well to extensibility.

That’s nice and all, but there’s an ugly elephant in the room: what happens when I have an object that produces a local event, and I want not only direct observers to get it, but indirect subscribers (via a bus)? This is why I said be ready to use both in tandem: you promote (or “bridge”) local events to the message bus as needed. Does this require more code? Initially, perhaps – but not as much as what you’ll end up writing to get yourself out of tightly-coupled hell if you’re passing your subjects directly to everything that needs to observe it. This is a good segue to the next two points…

3.) Selectively “Promote” Local Events to the Bus

Early on, I used the observer pattern almost exclusively for eventing in JavaScript. It’s the pattern we see over and over, and even popular client helper libs that are acting as mediator-hybrids, ultimately, present their API to us as if it’s the observer pattern. When I originally wrote postal.js, I then went through my “mediator all the things!” phase. It wasn’t uncommon to see publish and subscribe calls sprinkled through the prototypes and constructor functions I wrote. While I benefited from the de-coupled nature of this change, non-infrastructure code started to seem full of infrastructure concerns. Constructor functions taking a channel everywhere, subscriptions added as part of the new instance being created, prototype methods that published a value directly to the bus (where even a local subscriber had to listen through the bus to get the information instead of directly). Baking obvious bus-related concerns into these parts of the app began to feel like a code smell. The ‘narrative’ of the code seemed constantly interrupted by “Oh, and publish this out to all the subscribers now” and “WAIT! WAIT! Listen on this channel for that thing. OK, now keep going”. My tests suddenly required the bus as a dependency for low level unit tests. Something didn’t feel right.

The pendulum swung towards the middle and I began to realize I needed to preserve a ‘local API’ and extend the reach of data to the application through a mediator when necessary. My backbone views and models, for example, still used the normal Backbone.Events behavior for emitting events to local observers (i.e. – the model’s events were watched by its corresponding view). When other pieces of the app needed to know about that change in the model, I began bridging those local events to the bus along this line:

var SomeModel = Backbone.Model.extend({
  initialize: function() {
      this.on("change:superImportantField", function(model, value) {
          postal.publish({
              channel : "someChannel",
              topic : "omg.super.important.field.changed",
              data : {
                muyImportante: value,
                otherFoo: "otherBar"
              }
          });
      });
  }
});

It’s important to mention that while it’s possible to transparently promote events to the message bus, the local event and message should be treated as separate contracts – at the very least conceptually. In other words, you should be able to modify the ‘internal/local’ events without breaking the message contract. This is an important fact to keep in mind – otherwise you’ve simply provided a new way to tightly couple using an approach that aims to do the opposite!

So, the model above is testable without the message bus, sure. And my view and model could continue to function without any friction if I removed the logic bridging the local event to the bus. However, this is seven lines of boilerplate (albeit, formatted) . Bridging just four events results in almost thirty lines of code. OUCH. How do you get the best of both worlds – local eventing when it’s appropriate for direct observers, and extending the reach of events so that your subject doesn’t have to be passed around to everything – without the bloat? How can eventing have more taste and be less filling?

4.) Hide Boilerplate in Your Infrastructure 

It’s not that the code in the above example – bridging an event to the bus – is wrong syntactically or conceptually (assuming you buy in to the concept of local vs remote/bridged events). However, it’s a great example of the kind of friction good habits can bring to bear on a code base. “That’s too much code” will be said by someone at some point (as if LOC is the sole arbiter of a codebase’s quality). But, in this instance, I agree. It’s dreaded boilerplate. Here’s a pattern I’ve adopted in bridging local events on a Backbone object to postal.js:

// the logic to wire up publications and subscriptions
// exists in our custom MsgBackboneView constructor
var SomeView = MsgBackboneView.extend({

  className : "i-am-classy",

  // bridging local events triggered by this view
  publications: {
    // This is the more common 'shorthand' syntax
    // The key name is the name of the event. The
    // value is "channel topic" in postal. So this
    // means the bridgeTooFar event will get
    // published to postal on the "comm" channel
    // using a topic of "thats.far.enough". By default
    // the 1st argument passed to the event callback
    // will become the message payload.
    bridgeTooFar : "comm thats.far.enough",

    // However, the longhand approach works like this:
    // The key is still the event name that will be bridged.
    // The value is an object that provides a channel name,
    // a topic (which can be a string or a function returning
    // a string), and an optional data function that returns
    // the object that should be the message payload.
    bridgeBurned: {
      channel : "comm",
      topic : "match.lit",
      data : function() {
          return { id: this.get("id"), foo: 'bar' };
      }
    },

    // This is how we subscribe to the bus and invoke
    // local methods to handle incoming messages
    subscriptions: {
      // The key is the name of the method to invoke.
      // The value is the "channel topic" to subscribe to.
      // So this will subscribe to the "hotChannel" channel
      // with a topic binding of "start.burning.*", and any
      // message arriving gets routed to the "burnItWithFire"
      // method on the view.
      burnItWithFire : "hotChannel start.burning.*"
    },

    burnItWithFire: function(data, envelope) {
      // do stuff with message data and/or envelope
    }

    // other wire-up, etc.
});

You can obviously do this several different ways – with the bus framework of your choice – but it’s much less noise than the boilerplate, and might feel familiar to Backbone devs. When you control both the event emitter and message bus implementation, bridging can be easier. Here’s an example of bridging from a monologue.js emitter to postal.js:

// using the 'monopost' add-on for monologue/postal:
// assuming we have a worker instance that has monologue
// methods on its prototype chain, etc. The keys are event
// topic bindings to match local events to, and if a match is
// found, it gets published to the channel specified in the
// value (using the same topic value)
worker.goPostal({
    "match.stuff.like.#" : "ThisChannelYo",
    "secret.sauce.*" : "SeeecretChannel",
    "another.*.topic" : "YayMoarChannelsChannel"
});

Good habits are more enjoyable with boilerplate out of the way. Now I can test my objects locally, my bridging code independently, and even test that both together produce & consume the expected messages, etc.

It’s also important to note that if I needed to drop into the normal postal API in the above scenarios, nothing is stopping me from doing so. No loss of flexibility === WIN.

5.) Messages are Contracts – Choose Your Implementation Wisely

There are two general approaches in passing data to subscribers – and while there might be a more ‘official’ label for both, I’ve taken to describing them like this:

  • “0-n arguments”
  • “Envelope” (or “Single Object Payload”)

Consider these examples:

// 0-n args
this.trigger("someGuyBlogged", "Jim", "Cowart", "JavaScript");
// envelope style
this.emit("someGuyBlogged", {
  firstName: "Jim",
  lastName: "Cowart",
  category: "JavaScript"
});
/*
    In an emitter like monologue.js, the emit call above
    would actually publish an envelope that looked similar
    to this:
    {
        topic: "someGuyBlogged",
        timeStamp: "2013-02-05T04:54:59.209Z",
        data : {
            firstName: "Jim",
            lastName: "Cowart",
            category: "JavaScript"
        }
    }
*/

I’ve found, over time, that there was less and less friction (and code) involved in handling envelope style subscriptions vs 0-n args. The challenge with the “0-n args” approach arises primarily for two reasons (in my experience): first, the typical “Hey do you remember which args were getting passed when that event gets triggered? No? Ok, I guess I’ll go look at the source triggering it”. Not really a big deal, right? But it can break the narrative of the code. You can use a debugging tool, inspect the values at run time and try to infer the “labels” based on the values, but what’s easier – looking at an argument with a value of “1.21″ and wondering what it represents, or inspecting an object and seeing { gigawatts: 1.21 }. The second reason has to do with passing optional data long with the event, and the pains it exposes as method signatures grow longer.

“Really, Jim, you’re bike-shedding.” Perhaps. But I’ve seen code bases grow and morph over time, and simple innocent events with one or two arguments become monstrosities with optional arguments nestled in the midst:

// early on it was just this
this.trigger("someEvent", "a string!", 99);
// one day, it grew up and devoured us all
this.trigger("someEvent", "string", 99, { sky: "blue" }, [1,2,3,4], true, 0);
// BUT WAIT - the 4th and 5th args are OPTIONAL, so they might pass:
this.trigger("someEvent", "string", 99, [1,2,3,4], true, 0);
// Oh, you were checking that 5th arg for truthy/falsy?
// Whoopsies! It's now the previous arg...
this.trigger("someEvent", "string", 99, true, 0);

If any of the data is optional, there’s no getting around testing for it. But it requires less code, is more extensible and is typically more self-explanatory (thanks to member names) to perform that sort of checking on a single object literal being passed to the subscriber callback. I still deal with “0-n args” where I have to, but if it’s up to me, it’s envelopes all the way – both from my event emitters and in my message bus. (Proving my bias, monologue and postal both share the same envelope structure, minus the channel, which isn’t used in monologue.)

So – bear in mind that the structure used to deliver data to subscribers is part of the “contract”. With envelope style approaches, you have the ability to describe the event with additional metadata (without adding additional arguments) – which keeps the method signature (and thus that part of the contract) consistent for every event and subscriber.  You also have the ability to easily version a message structure (or add other envelope-level information as needed). Just be sure to use a consistent envelope structure if you go that route.

6.) Message “Topology” Matters More Than You Think

There’s no silver bullet here. But you need to be deliberate and thoughtful about how you name topics & channels as well as how you structure your message payloads. I tend to map my models one of two ways: on a single data channel, with the topic prefixed by the model type’s name, followed by it’s unique id then by it’s operation ({modelType.id.operation}), OR I given the model it’s own channel, and the topics are {id.operation}. A consistent convention assists in auto-wiring reply-to behavior when models make requests for data. But not all operations on a bus are requests. You might have simple events being published to the app. Are you naming the topic to describe the event (ideal), or are you falling into the trap of naming the topics to describe the intended behavior one of the subscribers should perform? For example, a message with a topic of “route.changed” vs “show.customer.ui”. One indicates an event, the other a command. Be deliberate as you make these decisions. Commands are not bad at all, but you’d be surprised at how much can be described by events before you need request/reponse or commands.

End Snapshot….

That was quite the mental download – if you made it this far, THANKS. While I hope that I don’t cringe at this six months from now, I’m encouraged that all of these points lend themselves to the goal of not building one large behemoth application, but instead building many small applications that interact via message passing. I hope – as frameworks develop and mature – that more of the boilerplate pain can be removed and greater discipline around these patterns in JavaScript will emerge and flourish…

Further Reading….

http://en.wikipedia.org/wiki/Message_broker

http://en.wikipedia.org/wiki/Observer_pattern

http://sourcemaking.com/design_patterns/mediator

http://www.eaipatterns.com/

Tagged with:  

What You Might Not Know About JSON.stringify()

On January 29, 2013, in JavaScript, by Jim Cowart

Nearly any developer spending even a moderate amount of time in JavaScript has had to, at some point, utilize JSON.stringify (and it’s counterpart, JSON.parse). JSON – JavaScript Object Notation – has become the go-to data-interchange format for many developers – with multiple languages capable of serializing to JSON, not just JavaScript itself. If you’re up late some night, unable to sleep, check out the history of JSON (tl;dr – Douglas Crockford is the brain behind it).

When writing in JavaScript, JSON.stringify is the way we take a value and serilalize it to a string value representing the object:

JSON.stringify({
    name: "Jim Cowart",
    country: "Jimbabwe"
});
// produces: "{"name":"Jim Cowart","country":"Jimbabwe"}"

JSON.stringify("Oh look, a string!");
// produces ""Oh look, a string!""

JSON.stringify([1,2,3,4,"open","the","door"]);
// produces "[1,2,3,4,"open","the","door"]"

I won’t belabor all of the rules of what gets serialized, you can read more about that here.  But it’s essential you know the following:

  • a value of undefined, a function or XML value are ommitted – except when….
  • If you have an array with undefined, a function or an XML value, it will be emitted as a null value

Let’s see about that:

JSON.stringify({
    doStuff: function() { },
    doThings: [ function() {}, undefined ]
});
// produces: "{"doThings":[null,null]}"

“Great, Jim. I get it. It’s a data interchange format. Behavior isn’t serialized. Most of the libraries I use these days handle serializing for me under the hood. So why write a blog post on JSON.stringify?! SHEESH!”

I know, right? Still – there are some nice tricks that can come in handy. Many of the larger applications I’ve worked on recently have had debug flags that can be flipped to enable various console logging from different components active on the page. While we obviously don’t want to sift through endless lines of console.log information all the time – when it becomes necessary, wouldn’t it be nice if it were a bit more readable?

// what if this:
'{"name":"Jim Cowart","location":{"city":{"name":"Chattanooga","population":167674},"state":{"name":"Tennessee","abbreviation":"TN","population":6403000}},"company":"appendTo"}'

// could be formatted like this, automagically?
"{
    "name": "Jim Cowart",
    "location": {
        "city": {
            "name": "Chattanooga",
            "population": 167674
        },
        "state": {
            "name": "Tennessee",
            "abbreviation": "TN",
            "population": 6403000
        }
    },
    "company": "appendTo"
}"

JSON.stringify actually takes 3 parameters (JSON.stringify(value [, replacer [, space]])), the 3rd of which – the “space” argument – allows you to specify either a string character to use for indentation, or a number. If you pass a number, that many spaces (up to 10) will be used for indentation:

var person = {
    name: "Jim Cowart",
    location: {
        city: {
            name: "Chattanooga",
            population: 167674
        },
        state: {
            name: "Tennessee",
            abbreviation: "TN",
            population: 6403000
        }
    },
    company: "appendTo"
};
// If you think the world only looks right through 2-
// spaced-indentation, then you'll appreciate this:
JSON.stringify(person, null, 2);
/* produces:
"{
  "name": "Jim Cowart",
  "location": {
    "city": {
      "name": "Chattanooga",
      "population": 167674
    },
    "state": {
      "name": "Tennessee",
      "abbreviation": "TN",
      "population": 6403000
    }
  },
  "company": "appendTo"
}"
*/
// If you're with "Team Tabs™", then you can
// kiss those space indentations goodbye by
// passing \t as the last arg
JSON.stringify(person, null, "\t");
/* produces:
"{
    "name": "Jim Cowart",
    "location": {
        "city": {
            "name": "Chattanooga",
            "population": 167674
        },
        "state": {
            "name": "Tennessee",
            "abbreviation": "TN",
            "population": 6403000
        }
    },
    "company": "appendTo"
}"
*/

So – what about the second argument, simply passed as “null” in the above example? That’s the “replacer” argument. It can be an array or a function. If you pass an array, it should contain the keys you want exported from the object:

// assuming the person object is the object from above
JSON.stringify(person, ["name", "company"], 4);
/* produces:
"{
    "name": "Jim Cowart",
    "company": "appendTo"
}"
*/

If you pass a function, it takes two arguments: the key and the value:

//  a bit contrived, but it shows what's possible
// FYI - the entire value being serialized is the first thing
// passed to the replacerFn, followed by each key on the
// object, hence the check to see if key is falsy
var replacerFn = function(key, value) {
    if(!key || key === 'name' || key === 'company') {
        return value;
    }
    return; // returning undefined omits the key from being serialized
}
JSON.stringify(person, replacerFn, 4);
/* produces:
"{
    "name": "Jim Cowart",
    "company": "appendTo"
}"
*/

You can use the replacer function approach to blacklist member names (in contrast to how the replacer argument as an array whitelists member names). I’ve found this approach to be useful when I need quick and targeted logging of certain values from a DOM element, but I want to prevent a serialization exception due to attempting to serialize a circular reference (which would occur if I drank some antifreeze and attempted to do something ridiculous like JSON.stringify(document.body)). Feel free to browse this fiddle for a simple example of using a replacer function to blacklist based on member name(s).

Of course, another option for customizing an object’s JSON serialization is to add a “toJSON” method to the object:

var person = {
    name: "Jim Cowart",
    location: {
        city: {
            name: "Chattanooga",
            population: 167674
        },
        state: {
            name: "Tennessee",
            abbreviation: "TN",
            population: 6403000
        }
    },
    company: "appendTo",
    toJSON: function () {
        return {
            booyah : this.name + ' , employer: ' + this.company
        };
    }
};

// The value returned from toJSON above is what
// will actually get stringified:
JSON.stringify(person, null, 4);
/* produces:
"{
    "booyah": "Jim Cowart , employer: appendTo"
}"
*/

“Seriously, Jim, there are bigger fish to fry than to write pretty JSON to a console.” You don’t have to convince me, I’m with you 110%. But I will say this, nicely formatted JSON makes a serious difference when you are wire-tapping a message bus for debugging visibility into the messages being published, or logging websocket payloads. Being able to selectively serialize objects by whitelisting member names with a replacer array argument (or blacklisting with a replacer function) can be very handy on DOM events/elements – helping you track down issues, or simply give you better visibility into complex interactions, without de-railing you with serialization exceptions.

Some further reading/exploring:
You’ll notice I never EVER said “JSON object” when describing JSON, or objects. Ben Alman has a great post explaining why “There’s no Such Thing as a JSON Object“. The only thing he fails to mention is that saying “JSON Object” kills kittens and puppies. By the dozens. True Story.

If you feel the need to write your own ‘circular reference safe’ JSON serializer in JavaScript, check out @getify’s idea here.

Also – kudos to Elijah Manor and/or Doug Neiner. I’m fairly certain one of them was the first to mention to me that JSON.stringify can format output.

So, go forth. Stringify.

Tagged with:  

Is JavaScript Tragically Important?

On September 1, 2012, in JavaScript, by Jim Cowart

devLINK 2012 Closing Keynote Panel

I had the honor of speaking at devLINK over the last 3 days – including the humbling opportunity to sit in on a ‘closing keynote’ speaker panel with Douglas Crockford, Bryan Hunter, James Kovacs and Elijah Manor. Needless to say, I feel woefully under-qualified sitting next to people of such caliber! John Kellar – founder and organizer of devLINK – invited the audience to ask questions. The attendees asked great questions and, at one point, someone asked Douglas Crockford if he thought JavaScript would still be the dominant language of the web in 10-15 years. His response:

“God, I hope not. If it turns out that JavaScript is the last programming language, that would be really sad. But unfortunately, because of its dominance in the web, it is now moving into virtually every place else. It has become a tragically important language, and we’re going to be stuck with it for a time….”

I wouldn’t be surprised if the mix of laughter during & after that statement was comprised of two reactions (at least):

  1. Nervous laughter from those who have embraced (and dare I say, loved) working in JavaScript. An accomplished, respected and knowledgeable figure just told them their baby was ugly. Awkward.
  2. Satisfaction (perhaps of the smug kind) from developers seasoned in other languages who’ve been dragged kicking and screaming (or still resisting being drawn) into this crazy new world of JavaScript’s ubiquitousness.

So – here’s my take on the situation. I was only around Douglas Crockford for a brief time today – but he struck me as someone who’s seen a lot of change and innovation throughout the course of his career. He’s seen languages rise and fall – and he definitely exudes the wisdom of someone who doesn’t view the development world through a myopic “one language” mindset. He cares enough about JavaScript to write a book that should be required reading for every JavaScript developer. He also cares enough to hope that innovation doesn’t stop where we are today. Due to the nature of the web, JavaScript has achieved a reach not many other languages have experienced – that’s important. But it lacks useful features which other languages have, has confusing quirks of its own, and evolution of the language is slow – that’s tragic.

So here’s what I’d say to my two strawmen:

1.) If you’ve embraced JavaScript – great! Work hard to innovate and make this space the best it can be for the period of time it dominates the web. But don’t get lulled into any false sense of security. If my own experience is any indication, a large majority of JavaScript developers either don’t possess, or are not applying, solid pattern knowledge and architectural principles. Do yourself a favor and learn these concepts so that you can apply them in any language. Play with other languages, even if it’s just a side project (and maybe consider learning a statically typed language).

2.) If you view the rise of JavaScript with anti-hipster-bitterness, you’re just experiencing what JavaScript developers will also one day experience: the fact that languages rise and fall, and yours may not be the “language du jour”. Consider the fact that very accomplished developers (from multiple language backgrounds) are delivering some incredible things right now in JavaScript. Don’t let the awful prevalence of spaghettified front end code trick you into thinking that “that’s just how JavaScript is”. No, it’s not. JavaScript – like so much of the web – has a low barrier to entry. And that’s a powerful reality. You can get a LOT done, without knowing a lot. But it’s developers (and would-be-developers) that are responsible for the spaghetti code, not the language. What we could benefit from is your knowledge being brought to bear in this space. And remember – a low barrier to entry doesn’t preclude the power it can wield.

I think the lessons conveyed over my time at devLINK this year are clear:

  1. Endeavor to do what you love
  2. Strive to become excellent at what you do
  3. Do *not* limit your skill set to only what your employer currently utilizes
  4. Learn another language.
  5. Repeat step 4.

That being said, it’s time for me to dust off erlang and Io again.

Here’s the panel discussion, if you’re interested:

 

Underscore – _.pluck

On June 25, 2012, in JavaScript, by Jim Cowart

Continuing what I hope to be an occasional series on underscore.js, I wanted to cover the “pluck” method in this post.

From underscore’s documentation, pluck has the following signature:
_.pluck(list, propertyName)

You can pass an array or an object to the first argument of _.pluck – if you pass an object, it iterates over the members of the object.

There’s really not much of a mystery as to what this function does. It’s pretty much a “_.map” shortcut – given a list of objects, you want to extract the values of a specific property on those objects into a new array. For example:

var addresses = [
    { city: "Nashville", state: "TN", zip: "37212" },
    { city: "Chattanooga", state: "TN", zip: "37406" },
    { city: "Signal Mountain", state: "TN", zip: "37377" },
    { city: "Snellville", state: "GA", zip: "30078" },
    { city: "Evergreen", state: "CO", zip: "80437" }
];
// get an array of the zip codes
var zips = _.pluck( addresses, "zip" );
// zips = [ "37212", "37406", "37377", "30078", "80437" ]

Simple, eh? I’ve been surprised to see many using this function only when they’re extracting primitives into an array, but using _.map to extract members that are objects (without any other modification as part of the extraction). If all you need to do is extract the object members as they are, pluck works just fine for that, too – and it saves a decent chuck of precious keystrokes:

var developers = [
    { name: "Jim Cowart", location: { city: "Signal Mountain", state: "TN" } },
    { name: "Elijah Manor", location: { city: "Nashville", state: "TN" } },
    { name: "John Papa", location: { city: "Orlando", state: "FL" } },
    { name: "Nicholas Cloud", location: { city: "St. Louis", state: "MO" } },
    { name: "Josh Bush", location: { city: "Nashville", state: "TN" } }
];
// the long way about it using _.map
var locations = _.map( developers, function( developer ) {
    return developer.location;
});

// the shorter way using _.pluck
locations = _.pluck( developers, "location" );

Anytime you can use ‘pluck’ in place of ‘map’, it might increase your code’s readability – especially if you’re using it conjunction with other operations. Consider this:

// getting an intersect of cities from two lists of locations, using map
var cities = _.intersection(
    _.map( locationsA, function( location ) {
        return location.city;
    }),
    _.map( locationsB, function( location ) {
        return location.city;
    }),
);

// getting an intersect of cities from two lists of locations, using pluck
cities = _.intersection( _.pluck( locationsA, "city" ), _.pluck( locationsB, "city" ) );
Tagged with:  

Underscore – _.reduce()

On March 23, 2012, in JavaScript, by Jim Cowart

It’s definitely no secret that I’m a fan of underscore.js. I’ve decided to write a collection of quick posts to highlight interesting ways in which underscore has proven useful. For today, let’s look at the “reduce” function.

From underscore’s documentation, reduce has the following signature:
_.reduce(list, iterator, memo, [context])

The docs describe it this way: “reduce boils down a list of values into a single value.  Memo is the initial state of the reduction, and each successive step of it should be returned by iterator“.

If you come away from that description thinking that reduce would be a powerful tool in working with numeric data – you are correct (in fact, underscore’s example presents it as such).  However, in a recent project, I ran into a situation where reduce made crawling an object literal a very simple task.  In this scenario, I had an object literal that provided state information for several components of the application.  Here’s a snippet of what the object looked like (changed & simplified, of course, to not reflect real data):

var info = {
    item1: {
        isRemote: false,
        active: false
        /* all kinds of other information for item1, etc. */
    },
    item2: {
        isRemote: false,
        active: false
        /* all kinds of other information for item2, etc. */
    },
    item3: {
        isRemote: false,
        active: false
        /* all kinds of other information for item3, etc. */
    },
    item4: {
        isRemote: false,
        active: false
        /* all kinds of other information for item4, etc. */
    }
};​​​​​​​​​​​​

This particular application also had an object that grouped the members of the info object together in various ways. For example:

var groups {
    group1: ["item1", "item2", "item4"],
    group2: ["item1", "item2", "item7"],
    group3: ["item1", "item3", "item4", "item6"],
    group4: ["item2", "item3", "item5"],
    group5: ["item2", "item3", "item4"],
};

Only one group of items would be active at a time. I wanted a clean way to switch between groups – effectively setting active = false for any item that didn’t belong in the new group being activated, but if an item was already active, and existed in the new group being activated, leave it alone. Below is an example of how you can use underscore’s reduce (in tandem with “each” and “difference”) to quickly produce the list of items that should be deactivated, etc.:

var activateGroup = function(groupName) {
    // get the items for this group
    var items = groups[groupName];
    // find out which items are active that aren't in the group we're activating
    var shouldDeActivate = _.difference(
        _.reduce(info, function(memo, val, key) {
            if (val.active) {
                memo.push(key);
            }
            return memo;
        }, [])
    ,items);

    // iterate over what we should deactivate and set the bool flag
    _.each(shouldDeactivate, function(itemName) {
        info[itemName].active = false;
        // our app did other work here to the component instance, etc.
    });

    _.each(items, function(item) {
        if(!info[item].active)
            info[item].active = true;
    });
};

Notice that when we call reduce, we pass in an empty array as the last argument – this is the initial state. As reduce iterates over the members of the info object, if the active property is true we push the key (the itemName) into the memo array (our state), and the memo is passed into the next iteration. Once reduce has “reduced” our list to only item names where active = true, the result is passed as the first argument to “difference” (an underscore function that takes two or more arrays and returns elements in the first array that don’t appear in the others). Our list of item names being activated is passed as the second argument to “difference”. This gives us an array of only active item names that are not in the list of item names we are activating. From there it’s simply a matter of iterating over the “shouldDeactivate” array, using the item name to look up the correct key on the info object, and setting active = false.

Hopefully this gives you an idea of how underscore’s reduce function can be useful to crawl an object, looking for members that match specific conditions. You could think of it, in this case, of being used like a selective “map” (in fact, you could easily replicate the same functionality by using “filter” and then “map” – but this does it in one operation).

The snippets above – while not doing anything “real”, are demonstrated here:

Tagged with:  

This is part 3 of a series. Parts 1 and 2 can be found here:

In parts 1 and 2, we looked at how a client-side message bus can help you decouple the components you use in a web application, enabling better testability & extensibility.  Now I’d like to look at a few common client-side messaging anti-patterns that I’ve run across (and…gasp…been guilty of).

1.) Mutating the Message

In a language like Erlang, the message passing style is said to be “immutable” – really meaning that the message sent is a copy, so that there is no shared mutable state between sender and recipient.  The challenge in JavaScript is that pub/sub message content is never a deep-copy-per-recipient (unless your pub/sub library enforces that….and many developers would rightfully balk at the overhead that deep-extending on each publish would entail as well as the constraints of what could be published).  This unfortunately leaves open the temptation to allow one of your subscribers to modify the message content – and since the subscribers initially share a reference to the same copy of the message, this can be a recipe for disaster.  Let’s look at an example:

var moduleA = {
    someOperation: function(itemToWorkOn, settings) {
        bus.publish("mySettings", settings);
        bus.publish("workItem", itemToWorkOn);
        appComponent.DoWork(settings, itemToWorkOn);
    }
};

// elsewhere in the app
bus.subscribe("mySettings", function(settings) {
    settings.someValue = (environment.lookThisUp) ? "OptionB" : settings.someValue;
    settings.subOptions = $.extend(true, app.defaults, settings.subOptions);
    // and all kinds of other message-mutating operations
});

// and yet again, elsewhere in the app
bus.subscribe("workItem", function(item) {
    if(item instanceof SomeType) {
        var stuff = item.oldField.split(' ');
        item.newFieldA = stuff[0];
        item.newFieldB = stuff[1];
        delete item.oldField; // ORLY?!
    }
    else {
        // more mutating atrocities here....
    }
});

// Oh hai, brand new subscriber...too bad it's going to fail
bus.subscribe("workItem", function(item) {
    $("#someElem").html("Currently Processing: " + item.oldField); // BUT Y U NO GIVE ME OLDFIELD?!
});

Now imagine that these snippets exist in different files…..in a large application…with multiple developers actively updating the codebase.  Have fun finding out why the brand new subscriber you added can’t access the “item.oldField” value on the message.

2.) Relying on Subscriber Priority for Critical Operations

The above anti-pattern (mutating the message) tends to lead naturally into this one.  If you’re allowing your subscribers to alter shared message content, then the order in which your subscribers receive messages becomes critical.  If you continue along these lines, and allow more than one subscriber to alter the message content, and downstream subscribers depend on upstream changes when they mutate the message, then you have no choice but to rule subscription priority with an iron fist.  “But that’s what susbcription priority is for!  Isn’t that what guaranteed order is about?”  First, guaranteed order is usually referring to the order in which multiple messages are delivered to one or more clients, not one message to multiple recipients.  Second, while I don’t have any problem with a pub/sub library supporting priority (after all, postal.js has it), I do think it’s a code smell if your application depends on subscriber priority for critical operations.  That tells me right away that your app is either a.) allowing subscribers to mutate the message, or b.) improperly handling immediate vs deferred publication of messages.  The brittle nature of this approach accelerates as new susbcribers are introduced (or new use-cases cause affected parts of the app to change).  It only takes one susbcriber to mess up the chain-of-dependent-message-mutations (or one developer to introduce a subscriber with the wrong priority).  This violates SRP & SoC on so many levels!  A wiser approach is to develop the application in such a way that subscribers treat the message as an immutable object.  If they need to alter it’s content for their own internal operations, then copy or extend the data onto something local so they are not dealing with shared references any longer.  If you’re using a pub/sub system to run a “transformation pipeline”, and you intend for each subscriber to alter the message content, my suggestion would be to try & use something lighter – like an array of functions that get called in order, each returning it’s transformed data for the next to consume.  On the other hand, using priority for non-critical operations is not only reasonable, but often times a very powerful tool (for example) in making the UI respond to changes in a particular way so the user doesn’t wonder if their input is being processed.  Just remember, any time you find yourself saying “Well, the only way that would work is if this subscriber has a priority of 17…“, then I can guarantee you there are better ways to solve the issue that will save you (your team and your app) from many headaches down the road.

3.) Too Much Of a Good Thing

While messaging is a powerful tool in decoupling, like anything, it can be overused.  For example, using messaging for internal events within a component – especially if it’s on a publicly accessible channel - is probably overkill.  When you’re communicating between ‘components’, though, messaging is a great fit.  For example, Backbone provides nice eventing between model and view.  Using a message bus like postal just to have a view’s model notify it of changes is a bit much!  However, using postal (or even your own custom pub/sub using Backbone’s events module) to handle communication between separate views (or models) enables your app to be free of tightly coupling all the disparate components that need to know about events external to themselves.  When I encounter a developer who’s extremely leary of pub/sub in the browser, it’s almost always because they’ve seen it overused in this way (coupled with the next anti-pattern below).

4.) Improper Abstraction of Subscribe and Publish Calls

This is a tough one.  As a general rule, this anti-pattern can be guarded against by constantly asking yourself two questions:

  1. Can I move this publish or subscribe call into my app’s infrastructure?
  2. Do I have pub/sub calls scattered throughout my entire codebase – making it difficult to determine where/when/why messages are being used?

The main symptom of this problem is when you see publish and subscribe calls everywhere, with no appearance of structure/rhyme/reason.  It usually boils down to not having a good differentiation between internal events in your module vs public events of which other components should be aware.

Another way to help avoid this anti-pattern is to write your component with a good public API without any assumption that messaging will be involved, and then write an extension/wrapper that ties that API into a message bus.  I’ve taken this approach with libraries like machina.js – in part because I want to provide developers not using pub/sub a good API experience, and because it helps me concentrate the message-bus-related code in one area, rather than it being littered everywhere in the codebase.  I’ve seen developers like Alex Robson take a similar approach (see cartographer) with good success.

5.) “Tightly-decoupled”

In a lively chat session with several developers about this post, one of the things we all lamented is seeing one version or another of what I jokingly called “tightly-decoupled” modules.  Perhaps the worst version of this is when module A uses messaging on a publicly accessible channel for its internal events, and module B subscribes to those.  Unless module B is a diagnostics wiretap printing feedback to the console, dare I say, you’re doing it wrong™?  When a module uses messages for public events, the message becomes the contract, so its understandable for your consumers to need tweaking if you change your public contracts.  But if you change module A’s internal pub/sub and it breaks module B, your modules are tightly decoupled – and in need of either non pub/sub internal eventing, or a private channel/sandbox local to the module.

6.) Not Enough of a Good Thing

Just as it can be overused, it can be under-utilized.  In discussing this post with Eli Perelman, he mentioned that he is often frustrated when a library doesn’t expose enough hooks to tap into.  “Just because you may not need control at that point doesn’t mean someone else won’t”, he said.  I agree.  I had this very frustration with version 1 of Knockout.js when I wrote a template engine plugin allowing developers to pull in external templates.  At the time, Knockout had a baked-in-assumption that the template binding lookup would be synchronous (since it was expected for templates to be in script elements on the page).  I nearly abandoned the support of my plugin, until I saw that version 2 of Knockout would have a better mechanism to support asynchronous retrieval.  Alex summed it up well:

“The problem is that most libraries are written as a tightly coupled block of calls vs. components.  In the end, I should be able to use your library naively, but I should also be able to understand it’s components and consume them as micro-abstractions.  I think it’s partly missing opportunities to encapsulate and decouple appropriately so that what you have is a nice conventional way to use a set of components that are decoupled and can be recombined or customized as needed.  Messaging helps address that.”

Next time I might just quote Alex the whole time!

Tagged with:  

machina.js – Finite State Machines in JavaScript

On March 12, 2012, in JavaScript, by Jim Cowart

I’ve been working on a helper framework recently, with the goal of providing a flexible means to write finite state machines (FSMs) in JavaScript.  I’ve borrowed some ideas from the world of Erlang/OTP (the gen_fsm behaviour is wonderful!), and the result of my efforts thus far is machina.js.

First – What is an FSM?

A finite state machine is an architectural model which can exist in only one of a finite number of states at a given time – and thus it responds differently to the same input depending on the state in which it is currently in.  A simple example is a traffic light.  The light responds to traffic differently depending on its state (red vs green) – obviously telling it to halt if the current state is ‘red’, and to proceed if the state is ‘green’.  This is a powerful pattern – capable of modeling and solving any number of problems at any layer of an application – and client-side web applications are no exception.  Consider some of the possible scenarios an FSM could address:

  • Application Initialization:  You might have a series of states in which your web application can exist prior to when the user should be allowed to interact with the interface.  During this time you might be pre-loading external templates, retrieving external data, rendering DOM elements dynamically, etc.
  • Offline vs Online: If your web application needs to support the ability to work offline, then you could use an FSM to abstract how (and where) data is stored given the two possible states.
  • “Grouped” Changes to UI: It’s quite possible that the UI in your web app changes configuration (i.e. – several views swapped in & out) based on where the user has navigated.  An FSM can abstract the “UI configuration” states, allowing you to simply say (via pseudo code) “transition into the ‘Customer Module’ state”, where the FSM will handle the smooth transition away from the current UI (which may involve hiding elements, undelegating events, animated transitions, etc.) and into the selected UI (which may involve showing new elements, retrieving external templates/data, transitions, etc.).
  • View Lifecycle: If you’re using a framework like Backbone.js (or any other) which has the concept of a view object, an FSM can wrap/manage the view’s lifecycle concerns – responding differently to messages based on one of several possible states (like “not-instantiated”, “not-rendered”, “shown”, “hidden”, etc.)

It’s certainly possible to (mostly) manage the above concerns using deferreds – but after having spent time in codebases liberally sprinkled with deferreds (many times, several layers deep of nested deferreds), I find that they very quickly can become difficult to test, difficult to debug and (most importantly), they lock together a series of steps which I may want to allow to only partially run.  For example, consider the “Application Initialization” scenario above.  If part of the initialization process is retrieving external data, I may want to allow the user to ‘refresh’ that data without re-triggering the entire init process again.  While this can easily be done with a second deferred (or set of them), if I’ve abstracted the problem with an FSM, I only have to transition back to the state where the app is retrieving data and allow it to process from there.  And I love being able to take advantage of the times that SOLID and DRY exist together in peace (when they make a nice deodorant….rooting out code-smells).

Erlang/OTP Influence on machina.js

I’ve had the good fortune to spend a decent amount of time over the last year learning about the mind-melting world of Erlang.  The OTP library for Erlang provides several “behaviours” (somewhat like a ‘template’ for creating a process that follows certain API conventions) – one of which is the Gen_Fsm behaviour.  There are two things that I think make Erlang’s FSM behaviour super powerful:  first, the current state is the name of the function that will get invoked, and second – Erlang’s pattern matching makes the way a state handles different messages amazingly concise, flexible and extensible.  Let’s look at a quick snippet.

If we have an Erlang FSM which has “initializing”, “loading” and “loaded” states, then we might see something like the following snippet:

initializing({load_customer, CustomerId}, State) ->
    NewState = State#state{ customer_id = CustomerId },
    {next_state, loading, NewState}.

loading({succeeded, Customer}, #state{customer_id = CustomerId} = State) ->
    event_module:notify("Successfully Loaded Customer ~p ~n", [ CustomerId ]),
    NewState = State#state{ customer = Customer },
    {next_state, loaded, NewState}.

loaded({add_order, Order}, #state{customer = Customer}) ->
    order_module:process_order(Customer, Order).

The above code shows three Erlang functions, each representing one possible way to handle a message in a given state. The “initializing” function above will only be invoked if the current state of the FSM is “initializing”, and if the first argument is a tuple which has the atom “load_customer” as the first part. There could actually be any number of “initializing’ function overloads, each handling a different (for example) tuple in the first argument – this is one small example of the beautiful world of pattern matching in Erlang. Note that at the end of the “initializing” function, we’re returning a tuple that tells the FSM to transition to the “loading” state, passing the “NewState” record along with it.

The ‘loading’ function example above would only get invoked if the FSM was in the “loading” state and if the first argument was a tuple, with the atom “succeeded” as the first part of the tuple. You probably get the idea from here. The concept of the current state value also being the name of the function to invoke to handle a message/event, and the ability to “match” the right function to invoke based on the value of arguments passed – these are two key ideas I’ve borrowed from Erlang/OTP in my attempt to write a decent JavaScript FSM framework. Oh that JavaScript could support pattern matching on method signatures!

UPDATE – Elijah Manor suggested I go ahead and include a brief JavaScript version of how the above erlang functions would look if converted to use machina.js (I was worried about post length, but if you’ve made it this far, thanks!):

var fsm = new machina.Fsm({
    states: {
        initializing: {
            // other handlers for the "initializing" state would go here
            load_customer: function(customerId) {
                this.customer_id = customerId;
                this.transition("loading");
            }
        },
        loading: {
            // other handlers for the "loading" state would go here
            succeeded: function(customer) {
                this.channel.publish("Successfully Loaded Customer " + customer.id);
                this.customer = customer;
                this.transition("loaded");
            }
        },
        loaded: {
            // other handlers for the "loaded" state would go here
            add_order: function(order) {
                orderProcessor.processOrder(this.customer, order);
            }
        }
    }
});

So How Did I Do It?

Machina.js organizes an FSM’s states into objects, where each state is an object literal which contains functions that are named to represent the kind of “message” or “event” they handle.   The state object – while obviously not a function – parallels the Erlang/OTP FSM in that only methods that live on that object will get invoked if the FSM’s current state value (which is a string) matches the member name that object is assigned to. The name(s) of functions that exist on a given state’s object parallel how Erlang pattern-matches to find the correct function to invoke by matching the function signature against the arguments actually passed.

For example, let’s say we have a machina.js-based FSM which is in the “ready” state.  If it receives a message telling it to handle “customer.getData” – or if someone directly invokes “handle(“customer.getData” /*, other args */) –  then it will look for “this.states.ready” (i.e.- do we have handlers for the “ready” state), and then ‘this.states.ready.[”customer.getData”]’.  If the handler is found, it invokes that function, passing in the arguments provided to the “handle” call (i.e. ‘this.states.ready[”customer.getData”](/* other args */).  A machina FSM can also have a “catch-all” handler (indicated by a “*”), which will handle any messages where a named handler doesn’t match the message/event type.  If the FSM can’t find a named handler or a catch-all handler, the message/event is simply ignored.

Moving Beyond the Basics

I’ve bounced a lot of my ideas for machina.js off of Alex Robson (who is a far better Erlang developer than I am, and is helping me “dog-food” machina in other projects).  Alex inspired additions to machina like the “deferUntilTransition” and “deferUntilNextHandler” calls.  Calling “deferUntilTransition()” inside a state’s handler will queue the current handler’s arguments up to be replayed after the FSM transitions into a new state.  This adds a lot of power to the FSM, as it enables you to not worry about when it receives messages, since you can defer them until the FSM transitions into a state that can appropriately handle the message.  The “deferUntilTransition” call takes an optional “stateName” argument, which will cause it to wait until the FSM has transitioned into a specific state and not just the ‘next’ state.  The “deferUntilNextHandler” call is similar, except it only waits until the next handler is invoked before it tries to replay the message, and does not wait for a state transition.

Another thing I intended for machina.js from the start was message bus integration.  Machina supports two JavaScript message bus providers out of the box: postal.js and amplify.js.  It’s worth noting that since postal.js supports exchanges and wildcard bindings, you get a little more power in integrating machina with postal, but either library allows you to communicate easily with the FSM via messaging.  (Good de-coupling, FTW!)  You aren’t  required to interact with machina.js via messaging, though, so feel free to use the API directly for both invoking state handlers (via the “handle” call) and subscribing to events (via “on” and “off”).  Also – if you have a favorite pub/sub library already, you can write a provider for it to integrate with machina.js – just look at the postal and amplify providers for reference.

“machina.utils” provides more extensions points, enabling you to alter default configuration options for how FSMs are created, and changing what your message “payloads” look like if you’re using machina in conjunction with a message bus provider. 

Parting Thoughts

I’ve been pleasantly surprised to hear of other JavaScript developers discussing the use of FSMs.  It’s strengthening my conviction that they are often a superior abstraction for solving certain probems than deferreds.  If this sounds interesting, then go check out machina.js.  There are two runnable examples included in the repository, so download, explore and have fun!  And then come back here and share your thoughts….