Benjamin W. Smith

Benjamin W. Smith

Benjamin W. Smith  //  Sysadmin by trade, Pythonista by passion. Dad to two boys and a girl. Guitarist. I like my coffee black, just like my metal.

Jan 18 / 6:56pm

stomp-js - STOMP in node.js

Intro

OK, so I suck at this.  I set out to write at least two, maybe three blog posts last night.  One of the other two is going to require much more thought and editing, the third is about vaporware (I literally have one test done) and this one, well, this one is the easiest because most of the work is already done ;).

So here we go with a quick run-through of stomp-js, an implementation of the STOMP 1.0 protocol in node.js.

I initially started out doing this as a simple exercise to learn node.js.  After toying with the code for some time, I realized I had something decent going on.  So I decided to clean it up, refactor some and release it.

Here we are now, with 0.0.2 of stomp-js.

Installation

Installation is pretty straight forward, assuming you already have node.js and npm installed.

npm install stomp

Not using npm?  Clone the github repo!

git clone https://benjaminws@github.com/benjaminws/stomp-js.git

Usage

First and foremost, we should setup a consumer to listen to the queue we will be hammering with messages later.

These examples assume you have a broker (ActiveMQ or RabbitMQ with the STOMP plugin) running on localhost, listening on port 61613.  To adjust the connection settings, change stomp_args, which looks something like this:

Taken almost directly from the project repo:

Pretty simple what's going on here, but lets step through it anyway.

  • Setup arguments to pass to the constructor
  • Create the object
  • Setup some options for subscribing to the destination
  • Connect to the broker
  • Subscribe to events
    • When 'connected' is fired, subscribe to the desired destination
    • When 'message' is fired, ack the message (you could do fancy things with the message here)
    • When 'error' is fired, inform of the error and disconnect
    • When 'SIGINT' is caught by the process, report a tally of work done and disconnect

 

This will listen for messages until we kill it, or if an error is encountered.  It's single purpose is to consume all the messages and acknowledge them with the broker.

Now we can't just let that consumer sit there being all lazy, lets send it some messages.

A stomp-js producer:

 

Stepping through this briefly.

  • Get number of messages to produce from the arguments
  • Decided if we want to get a receipt for messages produced
  • Setup arguments for the constructor
  • Create the object
  • Set the destination for messages
  • Connect to the broker
  • Subscribe to events
    • When 'connected' is fired, loop 'n' times, sending a message to the broker on each iteration.  Disconnect when complete.
    • When 'receipt' is fired, report the receipt ID
    • When 'error' is fired, inform of the error and disconnect
    • When 'SIGINT' is caught by the process, report a tally of work done and disconnect

 

Pretty simple stuff!

I'd like to point out that stomp-js does implement the basic transaction support defined by STOMP 1.0.  Below is some example code showing how to implement this.

Basically you call client.begin(), which returns a transaction id.  Then add a 'transaction' header to the message you're sending with the transaction id as the value.  Once the send is done, call client.commit(transaction_id).

Here is the full example:

Fin

That's basically it!

Want to help make stomp-js better?  Have an issue with it?  Want to keep up with progress?  Follow/Fork/Report on the github page! https://github.com/benjaminws/stomp-js

Filed under  //  gist   git   github   javascript   messaging   node   node.js   npm   stomp  
Feb 13 / 3:54am

jQuery rocks.

So I had a simple task that I wanted done with minimal code. I wanted to basically update the copy of dynamically generated images every few minutes. So, within a function that gets called every minute I wrote this:

$('img').each(
        function() {
            $(this).attr('src',$(this).attr('src'));
        }
    );

So easy, it should be illegal..

Filed under  //  code   geek   javascript  
Oct 31 / 11:59pm

Too much AJAX can stain your clothes..

Warning: Keep away from children, pets, eyes, and DON'T mix with other chemicals.

I'm of the opinion that the use of AJAX technology can be great, but has it's own practical application, much like the cleaning product. When something intrudes on my web browsing experience, I get annoyed. Much like when I get AJAX on my favorite jeans!

An example of this is Snap Shots. It's one of those services that grabs thumbnails of websites and when you roll over a picture, or link, it will give you a view of what to expect when you click it. Interesting idea, intrusive implementation IMO.

Something else that gets me going is a website that has a login "dialog" which fades the content behind it, makes it untouchable, and throws a login form in my face(On the same vein, I hate that in an desktop environment as well). I understand that there are probably some usability experts out there that would say this is a good way to login to a site, because it doesn't take your attention from the content underneath as much as a page loads and multiple clicks would. I understand that, but if I'm ready to login, I'm probably done with what I'm looking at and am ready to move on.

Another thing I've noticed lately is the amount of JavaScript you see on sites with AJAX, chewing up resources on my browser, pissing off my Firebug. Do you _really_ need a massive library behind all your fancy AJAX when slim, trimmed down Codebases like mootools and jQuery already do 90% of the stuff people are trying to accomplish these days! These are just a few of the things that annoy me about this web2.0 AJAX thing.

P.S. Gmail 2.0 rocks.

Filed under  //  annoyances   geek   gmail   javascript