Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn
Wojciech Galanciak
Senior Eclipse Developer on the MyEclipse and Webclipse products.
Posted on Sep 22nd 2015


The Internet of Things (IoT) is a fast growing market. The general idea behind IoT it is to connect everything (clothes, devices, houses, public transport, medical devices, etc.) into a set of networks to raise precision, productivity or even define a completely new user experience. This article focuses on different applications of IoT technologies, integration with different devices and utilization of different tools and frameworks.

MQTT Protocol

As defined, the core of IoT is a communication itself. In the modern Internet there is an impressive number of protocols and frameworks used to implement communication channels. This variety is drastically reduced in an IoT environment. In most cases, “things” that are communicating with one another have limited resources (processor, memory, battery, etc.). Fortunately, there are solutions for those restricted requirements. One of them is MQTT – a lightweight connectivity protocol.

According to, it was designed as an extremely lightweight publish/subscribe messaging transport. With this approach there is no direct connection between a publisher (a source of data) and a subscriber (access data requestor). Thanks to that, the publisher does not need to know anything about a data destination. It also does not need to handle any requests because responsibility for pushing data to the outside world is only on its side. The server role is played by a MQTT broker which:

  • receives subscription from publishers
  • receives messages from publishers
  • receives subscription from clients
  • pushes messages to subscribers

The only common part is a knowledge about the topic, an identifiable name of a particular messages group. For example, “home/widows/state” may refer to the state (opened, closed) of windows in an intelligent house. Each window has its own state detector. If the state changes, then the detector publishes a proper message with this concrete topic to a broker. Then the broker pushes it to all clients (e.g., a mobile phone application) subscribed to this topic which in this example can warn the owner if someone tries to break into his house.

It is important to mention that the subscriber does not know the source of a particular message. This means that all information required for window identification must be a part of the message itself. The message contains only one additional flag – Quality of Service (QoS). MQTT support three levels of QoS:

  • at most once
  • at least once
  • exactly once

To find more details about QoS please look at References section.

Eclipse Paho

There is a set of different implementations of MQTT protocol available on the market. We decided to focus on Eclipse Paho – open source implementation under the Eclipse Foundation. One of its main advantages is that it provides implementations for a wide range of languages/technologies, from JavaScript to embedded C. In this article we are focused only on two of them: Java and JavaScript.

The Idea

The purpose of this article is to present MQTT basis including a MQTT protocol in action. To do that we will create a web application which reads the temperature from a remote sensor device and displays it in a web browser. Instead of a real sensor device we will create a Java application to simulate its behavior. Please refer to the attached sample projects to follow along. 

Our goals are to:

  • Create a Mobile Hybrid Application which allows user to:
    • Connect/Disconnect with the device
    • Display temperature data published by the device
  • Create a mock Java sensor device which generates random temperature values in specified intervals.


Java Sensor Application

Let’s create the source of our temperature data. As mentioned earlier, we will use a Java mock application instead of a real sensor device. Eclipse Paho provides an easy method to use Java MQTT client implementation. The project structure is as follows:



It contains only two elements:

  • MqttPublisher – implements whole application logic
  • Paho Java MQTT library—In the Java client the central part is MqttClient class which provides API to manage the connection with a specified broker. In our case we use an open demo broker: This particular instance listens on port 1883 for TCP connections. In this case, the broker URI is: tcp://

The general workflow is described below.

  1. Create an MQTT client instance.
  2. Use MQTT client to connect with a specified broker.
  3. Subscribe to a custom topic (in our case it is: sensordevice/temperature).
  4. Publish a random temperature (from 20 to 30 degree) every second.
For details, refer to the inline comments in the code.

Web Application

Like the Java application, we will use Paho, but this time it is a JavaScript implementation of MQTT client. It is just a single library file (mqttws31.js) which needs to be added to a web application.

Here is the project structure:



JavaScript implementation is based on WebSockets for connection with a MQTT server. This approach requires a broker with native WebSockets support or a gateway which forwards communication between WebSockets and TCP.

The client application workflow is presented below.

  1. On startup, MQTT client instance is created.
  2. The Connect button selection triggers connect method on a MQTT client instance.
  3. If the connection is established then it automatically subscribes to our topic (sensordevice/temperature).
  4. The received temperature is displayed on a widget.
  5. Disconnect button unsubscribes from the topic and disconnects the client.

The user interface is very simple. It contains one Connect/Disconnect button where the label and color depend on the connection state. There is also a nice temperature widget. The whole UI is based on Bootstrap framework.


Both applications work independently. For example, you can open the web application and choose to connect. If there is no Java application running then the temperature widget will not be updated. Once it is started the temperature starts to change. 




This article included an introduction to MQTT protocol. The example communication is very basic and does not apply to a real scenario (e.g., see what happens if you launch two Java application instances).

Attachments—Sample Eclipse Java project that uses Eclipse Paho to simulate a sensor device.—A simple web application that connects to our simulated sensor to display temperature information.


MQTT Quality of Service –
Eclipse Paho –
Bootstrap –
Temperature Widget –



Related Posts

Benchmarking JavaScript: STS, Red Hat Dev Studio &... When working with JavaScript in Eclipse, whether you’re just editing a few files, or working on an advanced front end application, there are both performance and functional considerations you need to be aware of. Let’s take a couple of Eclipse based IDEs, and put them through their paces with a couple of applications, both without and with CodeMix....
Celebrating Success… with You! Happy Holidays! We are excited about closing off another successful chapter! You’ve made 2018 a wonderful year: our Darkest Dark theme is number one on the Eclipse Marketplace with over 600K installs, and CodeMix keeps gaining popularity with over 100K installs - currently number seven.  Thank you for giving us your preference!We are getting ready to celebrate, no...
CodeMix – Better with You! Thanks to detailed feedback from our awesome users, our team has been hard at work adding stability, reliability and performance fixes, and enhancements, to CodeMix. Read on for the deets on what’s new in CodeMix over the last month.Editing & ValidationWhen editing TypeScript files, error markers were sometimes not cleared when the error was fi...
A Day with Dani: Eclipse Plugin for Awesome Worksp... DevStyle Rescued My Workspace!As a new developer working in Eclipse, nothing was worse than doing a project and looking at an outdated, plain workspace. It was boring, and took away from the excitement and learning these projects had to offer. This was an IDE I would be using almost every day, so when I learned about the DevStyle plugin for Eclipse...