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.

9 comments:

  1. I can't build the mqtt-client succesful could you suply a download link to a working .jar file. Would really like to test the demoproject.

    ReplyDelete
    Replies
    1. Hi Bohsen,

      I just cloned the mqtt-client project and was able to build it successfully. The links to the jar files have also been updated on the project's git page: https://github.com/fusesource/mqtt-client

      -Jason

      Delete
  2. Your work article, blogs I mean over all contents is must read stuff.
    rooting android tablet

    ReplyDelete
  3. hi,
    I got the example running on my phone,
    but unable to connect to activemq broker,
    would like to know if there are any tools to debug.
    Or would there be anyway to know if the lib I've compiled using mvn is functioning correctly.

    Thanks.

    Lan

    ReplyDelete
  4. Thanks for sharing the demo application!
    I was wondering if you have tried using as-it-is the Callback interface of FuseSource MQTT client on Android in order to get the updates assynchronously?
    I got some problems when modifying your app to use callbacks and I have the impression that the issue is the different callback/thread handling on Android.

    If you have experimented with it and have any hints on how to make it work, please share =D

    ReplyDelete
    Replies
    1. I managed to get it working with callbacks, I forked it here https://github.com/tcarlyle/android-mqtt-demo and now I am working on a small application on top of it.

      Thanks for the demo.

      Delete
  5. i am able to run but to run 1 to 1 what are the changes required.

    Thanks,Arun

    ReplyDelete
  6. Last working on Android version of hawtdispatch is 1.14:

    compile 'org.fusesource.hawtdispatch:hawtdispatch:1.14'

    compile('org.fusesource.mqtt-client:mqtt-client:1.10') {
    exclude module: 'hawtdispatch'
    }

    ReplyDelete
  7. Hi,

    Thanks for sharing the demo application.

    Can you please help me understand how to create chat rooms (channels) using apache activemq? My use case is I need to create a chat application that can exchange messages with a single user or a group of users.

    Thanks,
    CK

    ReplyDelete