J5’s Blog

January 22, 2010

Getting kamaloka-js ready for a new release

Filed under: AMQP, JavaScript, messaging — J5 @ 6:10 pm

Those who are following AMQP know that work on version 1.0 of the protocol spec is happening right now. Previous versions of the API were heavily dependent on the protocol itself but with 1.0 around the corner a new messaging API has come along to help bridge the gap between 0.10 and 1.0 for the most common use cases.

In kamaloka-js, the JavaScript AMQP bindings, I have been working on implementing this API along with cleaning up the codebase significantly. Today I put the final touches on multi-part frame decoding as well as the dispatching code and hope to have a brand new release next week. Here is a look at both the old and new API:

Old kamaloka-js API as used in release 0.1.0

<script src="/static/Orbited.js"></script>
<script src="/javascript/amqp.protocol.js"></script>
<script src="/javascript/amqp.protocol_0_10.js"></script>
<script src="/javascript/qpid_amqp.js"></script>

<script>
    amqp_conn = new amqp.Connection({host: 'localhost',
                                     port: 9000});
    amqp_conn.start();

    // You should have your server generate a UUID since browser methods
    // are unreliable at best
    session = amqp_conn.create_session('not_a_great_id' +
                                       (new Date().getTime() +
                                        Math.random()));
    var remote_queue =  settings.remote_queue + session.name;
    session.Queue('declare', {queue: remote_queue});

    var queue = session.create_local_queue({name: '0'});
    var output_cb = function(msg) {
        console.log(msg.header.delivery_properties.routing_key +
                    ' sent ' + msg.body);
    }

    queue.subscribe({exchange: 'amq.topic',
                                remote_queue: remote_queue,
                                binding_key: 'com.j5live.#',
                                callback: output_cb});
    queue.start();

    // test a multi segment transfer
    setTimeout(function(){
        session.Message('transfer',{accept_mode: 1,
                                    acquire_mode: 1,
                                    destination: 'amq.topic',
                                    _body: 'Test Transfer From Browser',
                                    _header: {delivery_properties:{
                                                 routing_key:'com.j5live.test'
                                              }
                                             }
                                    });
         }, 10000);
</script>

New kamaloka-js API as used in the upcoming 0.9.1 release

<script src="/static/Orbited.js"></script>
<script src="/javascript/jsio/jsio.js"></script>
<script>
   jsio("import qpid_amqp as amqp");
   jsio("from amqp.protocol import register");
   // load the 0.10 version of the protocol
   register("amqp.protocol_0_10");

    amqp_conn = new amqp.Connection({host: 'localhost',
                                     port: 9000,
                                     socket_cls: Orbited.TCPSocket
                                    });
    amqp_conn.start();

    // You should have your server generate a UUID since browser methods
    // are unreliable at best
    session = amqp_conn.session('not_a_great_id' + (new Date().getTime() + Math.random()));

    var output_cb = function() {
        msg = this.fetch();
        console.log(msg.get('_header').delivery_properties.routing_key +
                    ' sent ' + msg.get('_body'));
    }

    var receiver = session.receiver('amq.topic/com.j5live.*');
    receiver.onReady = output_cb;
    receiver.capacity(0xFFFFFFFF);

     // test a multi segment transfer
     var sender = session.sender('amq.topic/com.j5live.test');
     sender.send('Test Transfer From Browser');
</script>

The new API will offer a lot more control, better dispatching and a simplified interface. Further integration with js.io is planned as well as full support for transactions and flow control.

[read this post in: ar de es fr it ja ko pt ru zh-CN ]

1 Comment

  1. [...] this post in: ar de es fr it ja ko pt ru zh-CN [...]

    Pingback by John (J5) Palmieri: Getting kamaloka-js ready for a new release | TuxWire : The Linux Blog — January 22, 2010 @ 8:56 pm

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress