Leon Anavi
IT Tips && Tricks

Internet of Things

Created: 05.03.2015 00:42 Last Modified: 05.03.2015 01:12 Views: 5067
Keywords: broker, Cloud, IoT, JBoss, MQTT, Node.js, OpenShift, port, publish, RedHat, subscribe

Set Up MQTT Broker with OpenShift

Introduction

MQTT is machine to machine connectivity protocol which is designed in a way to be appropriate for Internet of Things. Long story short: MQTT allows clients to subscribe and publish message related to a given topic and a broker handles and delivers the information.

OpenShift is an advanced cloud hosting platform from Red Hat. This article is influenced by Chistina Lin tutorials and presentations about OpenShift and messaging brokers. The provided information in this article will guide you how to create a platform as a services (PaaS) by setting up MQTT broker in the cloud using OpenShift, JBoss Fuse and A-MQ.

Install MQTT Broker

Make sure you are registered to OpenShift and follow the steps to set up MQTT broker:

  1. Login to OpenShift. If you are using the service for the first time and you do not have any other applications hosted in OpenShift then click Create your first application now. Otherwise just click Add Application...
  2. Select JBoss Fuse.
  3. Set a public URL.
  4. Wait for a while until the application is created and save the displayed credentials. They are important and you will need them later.
  5. Go to the console URL and login using the username and password provided on the previous screen.
  6. Switch from Container to Fabric, click the Runtime tab and select MQ
  7. Click Create Broker Configuration.
  8. Type in a name of the broker. Set its type to StandAlone and set the minimum instances to 1. After that click Create Broker.
  9. Wait for while and on the next screen click to create a container for the broker.
  10. Now it is time for configuration. Click Wiki and go to root | fabric | profiles | mq | broker | default.broker
  11. Have a look at the configurations at org.fusesource.mq.fabric.server-broker.properties
  12. Create broker.xml, copy and insert the following configurations in it, and save it.
  13. Go to Runtime tab and restart the container.
  14. When the container is running again click the icon on the right end of the line to connect to the container and check its log files.

Connect and Test the MQTT Broker

OpenShift client tools and node.js command line interface for MQTT will be used to verify that the MQTT broker is up and running. If should be possible to use these tools on any Linux distribution, Mac OS X or even Windows. Please follow the guidelines to publish and subscribe for MQTT topic:

  1. Install OpenShift client tools.
  2. Run the following command in a terminal to configure the tools:
    rhc setup
  3. Set up local access to the MQTT broker that is hosted at OpenShift with port forwarding. Replace container with the name depending your own configuration and execute the following command:
    rhc port-forward container
  4. Pay attention to output from the previous step and note the port for MQTT on the local host, most probably it will be 1883. In rare cases, if you have another MQTT broker running on your local system, the local port forwarded from the MQTT broker in OpenShift might be different, for example 1884.
  5. Ensure that node.js and npm are present on the system and install mqtt node.js client:
    npm install -g mqtt 
  6. Connect to the MQTT broker and subscribe for topic hello:
    mqtt sub -t 'hello' -h '127.0.0.1' -p 1883
  7. Open another terminal and publish a message on topic hello with text test. Verify that the message is received by the subscriber.
    mqtt pub -t 'hello' -h '127.0.0.1' -p 1883 -m 'test'

If the message has been received by the client subscribed to this topic then congratulations you have successfully set up MQTT broker in the Cloud with OpenShift and your Internet of Things can communicate through it.

The port forwarding is not convenient for real life cases, especially if the MQTT clients are working on embedded devices such as microcontrollers and it is recommended to use a SNI Proxy as explained here.

See Also

Red Hat OpenShift xPaaS - Simple MQTT with OpenShift, Part One, Create A-MQ broker with MQTT protocol on Openshift
Red Hat OpenShift xPaaS - Simple MQTT with OpenShift, Part Two, Simple client connecting to Broker on xPaaS
MQTT.js


  Home | About | Contact | Disclaimer | Sitemap 2009-2016 Leon Anavi. All rights reserved.