Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn
Profile photo of Wojciech Galanciak
Wojciech Galanciak
Senior Eclipse Developer on the MyEclipse and Webclipse products.


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

Introducing DevStyle – Rebooting EclipseR... Yesterday we released the first production version of DevStyle, a free add-on for Eclipse. DevStyle seeks to dramatically improve the Eclipse user experience for millions of developers with the introduction of modern developer ergonomics and the beautiful styling you’ve come to expect from products in 2017.Over the years, the Eclipse evolution has ...
Gradle vs Maven – in Search for the Perfect ... In your quest for the perfect build automation tool, you have probably come across Maven and Gradle - the two big players in the field. The decision here is no less tricky than picking just the right ingredients for that awesome holiday dish you might be thinking about preparing. Of course, each tool has its advantages and downsides, depending on y...
30% off sale, including Angular IDE and more! Step away from the leftovers and come take advantage of our Cyber Monday sale for Angular IDE, Webclipse, MyEclipse and more!   Thanksgiving is a great time for getting together with family, but perhaps more importantly, it’s about the SALES!  Whether it’s Buen Fin or Cyber Monday, we all love a bargain. Now is the time to get the software you alwa...
Working with Java 9 in Eclipse Note: This blog was updated on October 11th, 2017 to reflect the release of Oxygen 1a which includes Java 9 support.The Java 9 GA release is just a few weeks old, and you can start developing with Java 9 in Eclipse, right now! All you need to do is download a suitable Oxygen 1a (4.7.1a) based distribution for both Java 9 and JUnit 5 support. Please...

Posted on Sep 22nd 2015