Hands on MQTT
This chapter introduces the MQTT protocol, often used in the context of IoT.
Last updated
This chapter introduces the MQTT protocol, often used in the context of IoT.
Last updated
MQTT, short for Message Queuing Telemetry Transport is actually a machine-to-machine (M2M) connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging protocol. It is useful for connections with remote locations where a small code footprint is required and/or network bandwidth is limited. It is also ideal for mobile applications because of its small size, low power usage, minimized data packets, and efficient distribution of information to one or many receivers (more...). Basically MQTT is one of pillars of the Internet of Things.
This chapter is currently linking to MQTT Essentials by HiveMQ .
To start using the SimpleMQTTClient
class, you first need to add the library as a dependency to your project.
Do this by first adding the JitPack repositories to your pom.xml
file:
and then by adding the library itself as a dependency to the same pom.xml
file.
Now you should be able to import the classes into your application:
The UML class diagram of the SimpleMQTTClient
class is shown below:
To create a SimpleMQTTClient object, you can use any of the given contructors. The ones without a broker
will automatically connect to mqtt.labict.be
.
For example using the default constructor:
From this point on messages can be easily published to a given topic:
When subscribing to a topic, the client needs to know where to redirect the received messages. This is handled using an interface called IMQTTMessageHandler
. Implementing the messageArrived
method in your own classes, allows your objects to be registered as message receivers.
You can pass an object of a class that implements the IMQTTMessageHandler
explicitly, for example an FXMLController
:
Or you can use an anonymous inner class object: