MQTT (MQ Telemetry Transport) is a lightweight publish/subscribe messaging protocol. MQTT is used a lot in the Internet of Things applications, since it has been designed to run on remote locations with system with small footprint.
The MQTT 3.1 is an OASIS standard, and you can find all the information at http://mqtt.org/
This article will guide you into the various steps to run your first MQTT application:
- Install and Start a MQTT Broker
- Write an application that publishes messages
- Write an application that consumes messages
The source code of the sample application is available on GitHub.
- Apache Maven 3.x
Install and Start a MQTT Broker
You can find many MQTT Brokers, for this example I will use one of the most common broker Mosquitto.
Start the MQTT Broker with the default configuration
Publish and Consume messages
Open two terminal windows and run the following commands :
You should see the message
Hello world in the consumer/subscriber window.
Write your first MQTT Application
For this example I will write a small Java application, since it is the language that I am using in my global project.
Add the Eclipse Paho dependency to your Maven project
1 2 3 4 5
Publishing a Message
Publishing a message is quite easy, create a MqttClient and use it to post on a topic.
1 2 3 4 5 6
You have many other options, configurations that you can use when posting a message such as security, quality of service (QoS), and more; but in this post I want to simply show how easy is to publish and consume MQTT messages.
To consume messages you need to implement a
org.eclipse.paho.client.mqttv3.MqttCallback that will receive the message and used this Callback class in the MqttClient of the Subscriber application.
The Callback class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
This Callback class is used in the Subscriber application as follow:
1 2 3
Like for the publisher, I am using the broker and client without any option (QoS, security).
Build and Run the Application
1- Get the Sample Code
Clone the project from GitHub
2- Build the project with Apache Maven:
This project is a simple Java application that runs a publisher and subscriber using the Eclipse Paho library.
For convenience, the example programs project is set up so that the maven package target produces a single executable,
/mqtt-sample, that includes all of the example programs and dependencies.
3- Run the Subscriber
The subscriber will receive and print all messages published on the
4- Run the Publisher
Run the publisher with the following command, the second parameter is the message to publish
In this article you have learned how to:
- Install and start a MQTT Broker, Mosquitto
- Create a publisher and subscriber developed in Java
This article is very simple by choice, to quickly run your first MQTT Application. I wrote this article as part of a global IoT project I am working on that will capture devices data, publish them into MapR Converged Data Platform using MQTT and MapR Streams; this is why I used Java for the application. You can use any MQTT client library to build the publishers and subscribers.