Wednesday, August 1, 2012

Android: MQTT with ActiveMQ

I have been wanting to create a simple demo for a while that sends a message from an Android device to ActiveMQ.  With ActiveMQ 5.6 the broker was enhanced with the MQTT protocol.  The MQTT protocol is a very light weight publish/subscribe messaging protocol that is ideal for use in portable devices such as phones and tablets where a small footprint is needed and network bandwidth may be limited or unreliable.  So I decided to have a look at the mqtt-client library that Hiram Chirino has been working on to build a little demo app that can be used to publish and subscribe to a JMS topic.  This demo is just the basics and provides the starting point to building something such as a mobile chat application.

Building the Code:

The client library I used for my demo application is the FuseSource MQTT client.  This is a very nice library that supports Blocking, Future, and Callback/Continuation Passing based APIs.  The source for this library is available at the following github repo: mqtt-client.  To run the Android MQTT demo you'll need to clone this repo and build the mqtt-client.

 git clone https://github.com/fusesource/mqtt-client.git  

For the purpose of this demo we are only interested in the mqtt-client so you can change into the mqtt-client directory and run:

 mvn clean install  

You should then see the build was completed successfully.

Now that you have the required library built, lets go a head and download the Android MQTT Client.  The source for this demo is available in the following github repo: android-mqtt-demo.  This repo can be cloned using the following command:

 https://github.com/jsherman1/android-mqtt-demo.git  

Once you have the android-mqtt-demo cloned you can build the source within eclipse using the Android SDK.  To open the project in eclipse, use the new project wizard to create a new Android project and select "Android Project from Existing Code".  Browse to the src files you just cloned and click finish.

Now you will need to add the mqtt-client library to a libs directory in the android-mqtt-demo project so the library will be deployed to the emulator or Android device.  With the mqtt-client library added to libs directory the project should build successfully.  You can now deploy this to an Android device or run it from the Android emulator.  Note this may very depending on the ADT version you are currently running.  I am running with version 20.0.1.  In previous versions you could add the library to the build path and it would automatically get deployed.  For more information on this see the thread on Dealing with dependencies in Android projects.

Running the Demo:

Okay, so now for the fun part; running the demo. The first thing we need to do is start an ActiveMQ 5.6 broker instance that has the MQTT protocol enabled.  This can be done by modifying the broker's configuration file, conf/activemq.xml, as follows to add a mqtt transport connector:
 <transportConnectors>  
   <transportConnector name="openwire" uri="tcp://0.0.0.0:61616"/>  
   <transportConnector name="mqtt" uri="mqtt+nio://0.0.0.0:1883"/>  
 </transportConnectors>  

If you would also like to enable security you could add the simpleAuthenticationPlugin configuration as follows:
 <plugins>  
    <simpleAuthenticationPlugin>  
       <users>  
          <authenticationUser username="system" password="manager" groups="users,admins"/>  
          <authenticationUser username="user" password="password" groups="users"/>  
          <authenticationUser username="guest" password="password" groups="guests"/>  
       </users>  
    </simpleAuthenticationPlugin>  
 </plugins>  

Now you can start the broker and Android MQTT Demo.  When the activity loads enter the URL Address for the MQTT connector, enter the User Name and Password if needed and click connect.  Once connected enter a message and click send.  The application is listening for messages on the same topic so you should see the message appear in the Received text box.


This should also work for the Apollo message broker as well which has a MQTT implementation.