the blog for developers

Autowiring components to a message bus in Javascript

When writing AJAX and SOFEA/SOUI applications, it’s a best practice to use a client side message bus to integrate UI components. People recognized this as a best practice lately for GUI development with Swing. As a sidenote Werx was excellent for this but sadly is dead.

One of the ways to do message buses in Javascript is the OpenAjax Hub implementation or TIBCO PageBus.

Subscribing to messages on the bus is easy with the subscribe method:

jo.ListComponent = function() {
  OpenAjax.hub.subscribe("list.get") {
    function(event, publisherData, subscriberData) {
      // do something when message arrives
    }
}

When you have lots of those subscriptions though, they clutter up your code. Werx had the nice idea to autowire methods to messages and use the methods as message handlers. I thought I should do the same for Javascript and OpenAjax Hub.

jo.ListComponent = function() {
  this.$list_get = function(list) {
    // do something with list
  }
  // connect the methods
  jo.connect(this);
}

Some magic happens in the connect method.

jo.connect = function(object) {
  for (var element in object) {
    if (element.charAt(0) == '$') {
      var event = element.substring(1).replace(/_/g, ".");
      var callback;
      // we need to capture the scope
      (function() {
        var fn = object[element];
        callback = function(event, data) {
          fn.apply(object, [data]);
        }
      })();
      jo.Bus.subscribe(event, callback);
    }
  }
}

The same could be achieved with a map of message handlers which is easy with functions in Javascript whereas in Java it takes more boiler code. A solution with methods as messages handlers as seen above has the benefit though that the object can contain other helper methods and state, which a map of message handlers can not. Using autowired methods makes your message bus code cleaner and more readable.

Thanks for listening.

About the author

stephan Stephan Schmidt has been working with internet technologies for the last 20 years. He was head of development, consultant and CTO and is a speaker, author and blog writer. He specializes in organizing and optimizing software development helping companies by increasing productivity with lean software development and agile methodologies. Want to know more? All views are only his own. You can find him on Google +

You can leave a Reply here. Of course, you should follow me on twitter here.

You can share this post!
Do you want to tell others about this article? Use the social bookmark icons to submit this artice to the service of your choice. Thanks.

Leave a reply.

Comments

We had considered some form of auto-wiring in the OAA hub, but it’s too magical and it’s slow. As your example shows, you can easily add it on top of what’s there, but the speed of iteration and string manipulation on slow JS engines (IE 6 and 7 in particular) makes it relatively infeasible. Even implementing hierarchal dispatch in the OAA hub was somewhat controversial for similar performance reasons.

Also, what happens when you call connect() a second time? For a script that will get passed around the net billions of times a day we decided to be somewhat hands-off about this (as I also had done in the even-simpler Dojo pub/sub system).

There will be an update to the hub at some point and you can help us define what should be in it by joining OAA or the hub mailing lists.

Regards

stephan

Hello Alex, first of all thanks for the great work and thanks for the insights. I’ve been thinking about joining OAA, we’ll see. I hope the development goes on, an integrated message bus from front to back is the way to go.

I was tinkering with such an architecture in 2000 when we founded a wiki/blog/ontology/tagging (knowledge management) company, but technology was not ready. Great to see it come to reality.

Leave a Reply

What people wrote somewhere else:

Additional comments powered by BackType

Guide to CodeMonkeyism

Over the last 4 years I wrote many articles on this blog. To make it easier for you to find the relevant ones, I've organized them into topics.

Top 10

6 reasons why my VC funded startup did fail

Go Ahead: Next Generation Java Programming Style

Java Interview questions: Write a String Reverser

The dark side of NoSQL

7 Bad Signs not to Work for a Software Company or Startup

Is Java dead?

Scala vs. Clojure

Never, never, never use String in Java

No future for functional programming in 2008 – Scala, F# and Nu

Clojure vs Scala, Part 2

Java Developer

Is Java Dead?

Go Ahead: Next Generation Java Programming Style

Be careful with magical code

All variables in Java must be final

Never, never, never use String in Java

Bending Java: More readable code with methods that do nothing?

NoSQL Guy

NoSQL: The Dawn of Polyglot Persistence

The dark side of NoSQL

Essential storage tradeoff: Simple Reads vs. Simple Writes

Sharding destroys the goals of your relational database

The unholy legacy of databases

Startup/CTO

Development Dream Teams

6 reasons why my VC funded startup did fail

American vs. European style of Software Development

12 Things to Reduce Your Lead Time and Time to Market

The high cost of overhead when working in parallel

Essential storage tradeoff: Simple Reads vs. Simple Writes

Job Seeker

Another Good (Java) Interview Question

7 Bad Signs not to Work for a Software Company or Startup

Java Interview questions: Write a String Reverser (and use Recursion!)

Java Interview questions: Multiple Inheritance

As a Manager: What I value in developers

Top 10 Tips (+1) to Get a Pay Raise

Agilist

What Developers Need to Know About Agile

5 Practices Better to Change in Your Scrum Implementation

Scrum is not about engineering practices

ScrumMaster and ZenMaster: The joke of certification

What is Trans-Scrum?