Thoughts on MassTransit on other languages | Dru Sellers

:

This weekend I sat in another nodejs talk, I found myself thinking again about how I would implement mass transit in other languages; specifically those that are not statically typed. The typical use of masstransit in .net looks like this.

bus.Subscribe<Ping>(msg =>
{
    // do stuff with the ping message
});

What this does is instruct the bus to subscribe to the message named ‘Ping’ and when that message arrives it then passes it to the handler (that’s the anonymous function). Because .Net is both statically and strongly typed I know that they want a ping message, just by looking at the type name. MassTransit also has a bunch of code to handle routing that is also influenced on the type of the message. But in JavaScript I don’t have that luxury in the same way.  in javascript you might say:

bus.subscribe('ping', function(msg) {

/* do stuff */

});

So we don’t have a type name to key off of, only a string name. As someone that has learned to use every last scrap of metadata in my programming language its very bizarre to switch to a language that has so much LESS metadata available to me. I could try to improve that a bit with a constant so it would look like

bus.subscribe(MessageNames.ping, function(msg) {

/* do stuff */

});

Which does feel better, but its weird to me that its not as ‘tight’ as the .net experience. But then again, that is the whole point of dynamic languages, a dynamic environment.

The more that I ponder on it, the more I wonder if its just that I don’t have the patterns of thought necessary to structure large applications in non-static languages. what i need is a new way to derive the ‘name’ of the message in JavaScript. I am not content with the simplistic ‘string’ approach. I wonder if I can also apply the powerful concept of object scoping to mass transit in JS in the same fashion that we do in the .net version as well. I should be able to, there is an amazing amount of object scoping in the best practices of JS.

//module pattern

(function(global, bus) {
    //could auto register handlers using a known convention
    bus.loadFrom(global);
    bus.subscribe('msg name', function (msg) {
    }); // for one offs
})(window, busInstance);

Ok, I like that quite a bit. Not sure if the global is needed or not. I wonder what that would look like in CoffeeScript (tries for a bit and falls on face, must try later). I wonder if I could then do some magic around require.js or some other dependency resolver to complete the magic. Hmmm….

Overall I am much happier now. I am starting to see how I could use javascript in much the same fashion, I have identified some patterns of use that I would like to attack and better understand around versioning as well. Of course a lot of this is just academic for me, I still need a real problem to solve.

Happy Sunday

-d