MassTransit.js | Dru Sellers

:

This week I attended the CodeMash conference in Sandusky, OH. The conference was another excellent oppertunity to really reach out to other technologies and see what I could learn from them. And I had just that opportunity when I say a tweet from my buddy Karthik that he was going to pair with another friend of mine on the new hotness node.js. I quickly bounced out of my seat and ran upstairs to join in on the fun. As with learning any new language its helpful to have a problem to solve. As would happen I have recently written a naive port of my OSS project MassTransit for the Ruby language for my talk. Knowing that there wasn’t a whole lot to it I thought this would make a great problem to tackle.

For the purposes of this small port I am focusing on the core set of functionality. Subscribe to messages, execute callbacks when messages arrive, and publishing messages. This means that I also have to be able to serialize the objects as well.

The transport we used was RabbitMQ, it’s one of the newer queuing systems out there that is built on erlang. You can read all kinds of good stuff about it just using the goog’s so I encourage you to do that.

First we had to go get ‘npm,’ the node package manager, (sweet, npm is on homebrew) once we had that we pulled down the ‘amqp’ package which allows us to talk to rabbitmq. Sweet.

Next up we wrote the ./transports/amqp.js file/package. And here I hit my first “Oh, crap! This is different.” experience in JavaScript. Namely, they don’t really have classes. You really just have a bunch of untyped objects running around that are really just glorified dictionaries. (take a look) After bmavity schooled my in the ways of JS class creation, which still hurts my head, we got our javascript talking to rabbit! Boom! whoo hoo. I knew that we would soon have something banged out. We continued on next with the Envelope class that I usually write but with the complely protypical nature of the language it proved to be unnesscary. The envelope class is usually nothing more than a container for the message name and the actual message data. With JS this class was overkill since to just make one

was as much code as the class definition. Which brings up, it was weird to serialize js objects to string based forms in JSON. Unsuprisingly, it was stupid easy to do this, so our serializer.js was also very easy to write.

Next and finally is the serviceBus.js module. this module aggregates the callbacks by message type, so I was able to learn about dictionaries and lists, and how to construct larger classes.

At this point I started to run into some weirdness in how the amqp library worked, and my lack of understanding in how node.js / javascript really worked. This is also where Mr. Mavity stepped in and started to kick some butt. You can check out everything we did on github here. Pretty excited to have this, now if we can get this to work with socket.io we will have a way to actually talk to the browser!!

:)