Feb 18 2008

Integrating Flex and RabbitMQ using STOMP

Tag: Flex, Messaging, RabbitMQ, STOMPDerek Wischusen @ 12:21 am

An experimental STOMP adapter was recently released for RabbitMQ. This means that it is now possible for Flex/Flash apps to communicate with RabbitMQ using the ActionScript 3 STOMP client. The following is a simple demonstration of how to get these technologies to work together.

First, let’s start with a quick description of RabbitMQ and STOMP.

RabbitMQ:

RabbitMQ is a complete and highly reliable Enterprise Messaging system. The RabbitMQ client libraries and broker daemon can be used together to create an AMQP network, or used individually to bring the benefits of RabbitMQ to established networks.

Packages/installers are available for all major operating systems and platforms. RabbitMQ can also be deployed as a VMWare/Debian virtual appliance.

Features:

* A complete, conformant and interoperable implementation of the published AMQP specification
* Based on a proven platform, offering exceptionally high reliability, availability and scalability
* Good throughput and latency performance that is predictable and consistent
* Compact, easily maintainable code base, for rapid customisation and hot deployment
* Extensive facilities for management, monitoring, control and debugging
* Licensed under the open source Mozilla Public License

STOMP:

The Stomp project is the Streaming Text Orientated Messaging Protocol site (or the Protocol Briefly Known as TTMP and Represented by the symbol :ttmp).

Stomp provides an interoperable wire format so that any of the available Stomp Clients can communicate with any Stomp Message Broker to provide easy and widespread messaging interop among languages, platforms and brokers.

As you can see from its description, the primary protocol that RabbitMQ uses is AMQP. If you’d like to learn more about AMQP, then check out this site.

There is an ActionScript 3 client for AMQP that is being developed by Ben Hood. You can read more about this client and learn how to use it by checking out this post on Ben’s site.

Alright, let’s get started with the example of how to use as3-stomp client with RabbitMQ.

1. Follow the steps in this tutorial to get RabbitMQ running with the STOMP adapter.

2. Download the project files for the Flex app.

This project consists of two separate Applications: the ImageSender and the ImageReceiver. The project file also contains the compiled as3-stomp library, so you do not need to download it separately.

3. Import the project into Flex Builder. (This step is optional. Alternatively you could unzip the project file and run the apps by opening the ImageSender.html file and the ImageReceiver.html file in the bin-debug folder.)

4. Launch both applications.

Before we proceed let’s take a quick look at what happened when we launched the apps.

Both apps have an init method that gets called when the application first the creationComplete event. Here is what this method looks like in the ImageSender app:

private function init () : void
{
    var ch: ConnectHeaders = new ConnectHeaders();
    ch.login = "guest";
    ch.passcode = "guest"
    stomp.connect("localhost", 61613, ch);
}

In this code we create a ConnectHeaders object that we use to set the login and passcode for this session. By default, RabbitMQ creates a user called “guest” with password “guest”. We then call connect on the stomp client. The stomp client was instantiated using an MXML tag like so,

<stomp:Stomp id="stomp"  />

The code for the init method in the ImageReceiver app looks pretty much the same except that it contains one more line.


private var destination: String = "/queue/images";

private function init () : void
{
    var ch: ConnectHeaders = new ConnectHeaders();
    ch.login = "guest";
    ch.passcode = "guest"
    stomp.connect("localhost", 61613, ch);
    stomp.subscribe( destination );
}

On the final line in the init method we call the subscribe method on the stomp client. By calling this method we tell the stomp broker (RabbitMQ in this case) that we want to consume messages that are sent to this destination.

The code where we instantiate the stomp client in the ImageReceiver is also pretty much the same as it is in the ImageSender,

<stomp:Stomp id="stomp" message="handleMessages(event)"  />

The only difference is that we set message event property so that every time a message event is fired the handleMessages method gets called.

Ok, back to the demo.

5. Go to the ImageSender app and click the Images button.

6. Select an image from the list and use the mouse cursor to draw some lines on the image.

Stomp Image Share ImageSender

7. Click Send Image.

8. Go to the ImageReceiver app and you should see the image that you sent.

Stomp Image Share ImageSender

Ok, back to the code. When you clicked the Send Image button the sendImage method was called,

private function sendImage():void
{
    var image: ByteArray = ImageSnapshot.captureImage(canvas).data;
    stomp.send(destination, image);
}

In this method we use the ImageCapture class (FYI: this class is only available in Flex 3) to capture the image data from that canvas that contains our image and store it in a ByteArray. We then call send on the stomp client to send this data to the “/queue/images” destination.

RabbitMQ receives this message and sends it along to any consumer that are subscribed to this destination, including our ImageReceiver app.

When the stomp client in the ImageReceiver receives the message it fires a message event and calls the handleMessage method.

private function handleMessages(event : MessageEvent) : void
{
    var bd: BitmapData = new BitmapData(canvas.width, canvas.height);
    var loader : flash.display.Loader = new flash.display.Loader();
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onBytesLoaded);
    loader.loadBytes(event.message.body);
    function onBytesLoaded (event : Event) : void
    {
        var content : DisplayObject = LoaderInfo( event.target ).content;
        bd.draw( content );
        canvas.graphics.beginBitmapFill(bd);
        canvas.graphics.drawRect(0,0, canvas.width, canvas.height);
        canvas.graphics.endFill();
    }
}

This method grabs the body of the message (which is the ByteArray containing the image data) and draws it on to a canvas.

That’s it. As always, let me know if you have any questions. And if you encounter any bugs that appear to be in the as3-stomp client, please submit a report here.


Discount viagra usa
Tramadol medication
Levitra purchase
Xanax with no prescription
10mg prednisone
Cialis 20mg side effects
Tramadol for sale
Online prescriptions xanax
Propecia best prices
Phentermine cheap online
Cialis 10mg side effects
Free samples of cialis
Cheap tramadol overnight delivery
Phentermine diet pills without prescription
Buy levitra
Buy viagra australia
Buy cheap valium online
Order xanax online
Australia viagra prescription
Buy viagra uk online
Cialis uk sales
Propecia usa
Buying valium in spain
Buy phentermine no script
Discount viagra online
Purchase xanax
Where to buy cialis safely
Tramadol prescription online
Viagra canada online
Propecia ireland
Where to buy viagra online
Phentermine 37.5mg
Buy viagra in england
Buy propecia online without a prescription
Buy viagra online cheap
Overnight tramadol no prescription
Prednisone 20mg side effects
Buy xanax 2mg no prescription
Cialis for sale
Levitra price
Tramadol pharmacy
Tramadol without prescription
Buy tramadol hcl
Buying prednisone online
Viagra prescription cost
Phentermine canada no prescription
Buy cheap viagra online uk
Xanax bars dosage
10mg valium effects
Xanax for sale without prescription
Where to buy phentermine cheap
Order tramadol overnight
Cheap xanax for sale
Order tramadol online overnight
Valium from india
Purchase tramadol without prescription
Dosage of xanax
Viagra pharmacy prices
Xanax 0.5 mg
Low price viagra
Order tramadol online cod
Where to buy cialis online
Buy generic valium
Propecia 1mg generic
Valium online fast delivery
Buy viagra from canada
Cheap phentermine without prescription
Levitra canada
Buy cialis brand
How to buy valium without a prescription
Viagra discount prices
Get tramadol prescription
Best viagra dose
Buy valium no rx
Buying viagra online
Viagra sale uk
Levitra online cheap
Viagra without prescription uk
Generic xanax
Generic cialis uk
Buying tramadol in uk
Buy generic phentermine online
Buy xanax online without prescription
Genuine viagra online
100mg tramadol online
100mg tramadol effects
Viagra india price
Cialis online canadian pharmacy
Viagra ordering
Propecia uk pharmacy
Viagra in usa
Cialis medication
Viagra generic cheap
Viagra dosage information
Order cheap viagra online
Viagra canadian online pharmacy
Cialis order online
No prescription valium
Buy phentermine no rx
Cheap levitra no prescription
Cost of viagra 50mg
Propecia generic online
Cialis purchase online
Best way to buy viagra online
Cheap 37 5 phentermine
Viagra buy online no prescription
Phentermine buy australia
Prednisone dosages
Viagra super active
Buying cialis online without a prescription
Get viagra prescription
Cheap 100mg viagra
2.5mg cialis
Canada pharmacy valium
Viagra tablets for sale
Valium pill 10mg
Generic viagra for sale
Viagra professional online
Tramadol online overnight
Valium no rx
Valium online overnight
Valium drug side effects
Viagra discount coupons
Purchase phentermine online
Buy viagra online in ireland
Where can i buy viagra without a prescription
Overnight xanax delivery
Prescription free viagra
Buy propecia
No prescription cialis online
Buy phentermine 37.5mg pills
Xanax generic dosage
Cheapest levitra
Valium online uk
Viagra in france
Order cheap phentermine
Propecia generic canada
Buy brand name viagra
Xanax no prescription overnight
Low price cialis
Cialis prices uk
Brand name cialis
Where to buy viagra in england
Prednisone 40 mg
Get viagra
Valium generic
Xanax overnight cod
Buy viagra online in australia
Buy valium europe
Buy tramadol cod
Cheap viagra online without prescription
Buy xanax overnight
Generic tramadol
Best way to take tramadol
Buy tramadol overnight
Viagra in the philippines
Viagra 50 mg online without prescription
Canada viagra
Cialis samples canada
Valium without prescription
Buying viagra in london
Tramadol dosage
Buy cialis in the uk
Valium cheapest
Phentermine online uk
Xanax 1mg
Xanax no prescription required
Tramadol without prescription overnight delivery
Order viagra without prescription
Viagra for sale online
Viagra online purchase in india
Viagra online cheap
Buy generic valium online
Cialis order canada
How to buy phentermine online
Viagra fast delivery
Phentermine hcl no prescription
Cheap valium online
Viagra purchase uk
Viagra online without prescription reviews
Propecia online uk
Prescription viagra canada
Where can i buy viagra without prescription
Phentermine online free shipping
2mg xanax no prescription
Viagra indian pharmacy
Authentic phentermine 37.5
Cheap xanax bars
Buy cialis online from canada
Viagra in the uk
Phentermine with no prescription
Order phentermine online no prescription
Buying cialis
Best price on phentermine
Purchase phentermine
Tramadol no prescription required
Buy generic xanax no prescription
Order prednisone no prescription
Prescriptions for phentermine
Viagra prescription
Levitra samples
Buy xanax cheap online
Order tramadol cod
Cheap cialis viagra
40 mg prednisone side effects
Where to buy levitra online
Buy phentermine hcl 37.5 no prescription
Buy viagra uk no prescription
Cheapest place to buy viagra online
Purchase tramadol online
Viagra price canada
Prednisone tablets 10 mg
Levitra us
Xanax bars effects

Best prices for cialis
Xanax 1 mg dose
Propecia uk prices
Prescription valium
Buy phentermine online no prescription
Phentermine without a prescription
Viagra pharmacy uk
Xanax online cheap
Buy propecia cheap
Cialis 20mg
Best levitra prices
Tramadol india
Cialis 20 mg dosage
Dose of xanax
Phentermine canadian pharmacy
Prescription phentermine online
Online prescription tramadol
Tramadol no prescription overnight delivery
Cheap tramadol cod
Tramadol cheapest
Buying levitra without prescription
Propecia information
Pharmacy tramadol
Low cost cialis
Viagra express delivery
Buy valium without prescription uk
Cheapest cialis professional
Propecia cost
Cheap cialis
Cialis price
Buy viagra 100mg
Generic levitra uk
Buy xanax canada
Buy pfizer viagra without prescription
Xanax 1mg side effects