RabbitMQ
SpringSource
  • Installation
  • Documentation
  • Get Started
  • Services
  • Community
  • Blog
« Exchange to Exchange bindings
Chapter 1: Introduction to Distributed Systems »

rabbitmq + node.js = rabbit.js

For those who have been away from the internets, node.js is an evented JavaScript engine based on Google's V8. Because it is essentially one big, efficient event loop, it's a natural fit for programs that shuffle data backwards and forwards with little state in-between. And it's fun to program, an opinion apparently lots of people share, because there have been loads of libraries crop up around it.

Among the more impressive of these libraries is Socket.IO. One can combine Socket.IO with node.js's built-in web server to make a websocket server, with a socket abstraction for browsers that degrades to XHR tricks for when there's no websockets. (I would be happy to believe that node.js and Socket.IO were made for us by a benevolent and foresightful precursor race; but of course, they were made by hard-working clever people. Thank you, people!)

Once one has a socket abstraction in the browser, a whole world opens up. Specifically, for our purposes, a whole world of messaging. Since node.js has an AMQP client, we can easily hook it up with RabbitMQ; not only to bridge to other protocols and back-end systems, but also to provide messaging between browsers, and between application servers, and so on.

Following on from the work we've been doing with Martin Sustrik of ZeroMQ, I decided to make a very simple protocol for using on the browser sockets, reflecting the messaging patterns used in ZeroMQ (and thereby in RMQ-0MQ) -- pub/sub, request/reply, and push/pull (or pipeline). I wrote a node.js library that uses RabbitMQ to implement message patterns using its routing and buffering; the bridging then comes for free, since RabbitMQ has a bunch of protocol adapters and clients for various languages.

A brief explanation of the messaging patterns:

Publish/Subscribe is for the situation in which a published message should be delivered to multiple subscribers. In the general case, various kinds of routing can be used to filter the messages for each subscriber. This might be used to broadcast notifications from a backend system to users' browsers, for example.

Request/Reply is for RPC over messaging; requests are distributed round-robin among worker processes, and replies are routed back to the requesting socket. This might be used by browsers to query back-end services; or even for browsers to query each other.

Pipeline is for chaining together processes. Messages are pushed to worker processes in a round-robin, which themselves may push to another stage of processing. This might be used to co-ordinate a workflow among sets of users (or indeed individuals).

Having duly dispensed with ado, here is rabbit.js.

All it needs is a bare-bones RabbitMQ and node.js installed; and, the node-amqp and Socket.IO libraries. Instructions and the locations of these things are in the README. (Do note that you need my fork of node-amqp.)

It also includes a tiny message socket server; that is, a node.js server that accepts socket connections and speaks in length-prefixed messages. Since it's all going through RabbitMQ, you can talk to the browsers hooked up with Socket.IO via a socket. You can also use the in-process pipe server from code running in node.js itself.

All in all, I am surprised how much I could get done with only a handful of lines of code and some technologies that each hit a sweet spot -- node.js for fun network server programming, Socket.IO for magical browser sockets, and RabbitMQ for the no-tears messaging.

Tags: javascript, node.js, rabbit.js, socket.io, websockets

This entry was posted on Friday, November 12th, 2010 at 11:18 pm by Michael and is filed under Hasenwerkstatt, HowTo. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

7 Responses to “rabbitmq + node.js = rabbit.js”

  1. Twitter Trackbacks for RabbitMQ » Blog Archive » rabbitmq + node.js = rabbit.js - Messaging that just works [rabbitmq.com] on Topsy.com Says:
    November 13th, 2010 at 12:47 am

    [...] RabbitMQ » Blog Archive » rabbitmq + node.js = rabbit.js - Messaging that just works rabbitmq.com/blog/2010/11/12/rabbitmq-nodejs-rabbitjs/ – view page – cached RabbitMQ is a complete and highly reliable enterprise messaging system based on the emerging AMQP standard Tweets about this link [...]

  2. Rabbitmq + node.js = rabbit.js – Messaging that just works | Techarama Says:
    November 13th, 2010 at 11:39 am

    [...] View full post on Hacker News [...]

  3. Josh Mather » Bookmarks for October 29th through November 15th Says:
    November 15th, 2010 at 6:19 pm

    [...] RabbitMQ » Blog Archive » rabbitmq + node.js = rabbit.js - Messaging that just works - [...]

  4. PHP Inter-process communication to monitor message queue - Programmers Goodies Says:
    September 7th, 2011 at 12:26 pm

    [...] RabbitMQ.com has a list of tutorials, libraries, and docs for PHP. Rabbit.js seems to be a good bet for Node.js. [...]

  5. Chris Adkin Says:
    October 27th, 2011 at 8:20 am

    I've attempted to run the example ( example/socketio.js ) provided with rabbit.js and I get:-

    node.js:202
    throw e; // process.nextTick error, or 'error' event on first tick
    ^
    Error: Cannot find module 'sockets'
    at Function.resolveFilename (module.js:334:11)
    at Function.
    load (module.js:279:25)
    at Module.require (module.js:357:17)
    at require (module.js:368:17)
    at Object. (/home/chris/rabbit.js/example/socketio.js:8:13)
    at Module.compile (module.js:432:26)
    at Object..js (module.js:450:10)
    at Module.load (module.js:351:31)
    at Function.
    load (module.js:310:12)
    at Array. (module.js:470:10)

    It would appear that node.js is expecting sockets to be a module, however I cannot find any reference to a sockets node js module on github. Can someone please point me in the right direction so that I can get this working.

  6. scworldnetter Says:
    October 27th, 2011 at 9:59 pm

    This looks really interesting, but when I went to try to work with node.js and the AMQP client, the client library have very cursory documentation at best, so really only the authors of the AMQP client can use the library effectively, for anyone else, it's pretty much hopeless. None of the code actually works out of the box, and requires major tweaking. Does anyone have a reference to a good blog or API documentation for this? Something similar to Javadoc? The one or two blogs I did find had code that was so buggy it was also pretty much useless.

    I would really appreciate a good reference for this.

  7. Michael Says:
    October 27th, 2011 at 10:59 pm

    scworldnetter: Are you complaining about the AMQP library or about rabbit.js, or both? It is difficult to tell.

    For the AMQP client, what is wrong with the README? https://github.com/postwait/node-amqp#readme
    (By the way, rabbit.js will work with npm-installed node-amqp now).

    For rabbit.js, the examples run with node v0.4.x, but may not work with 0.5. Sorry about that.

The postings on this site are by individual members of the RabbitMQ team, and do not represent VMware’s positions, strategies or opinions.

Sitemap | Contact

Copyright © 2012 VMware, Inc. All rights reserved. Terms of Use, Privacy