Eclipse Paho is an umbrella project which provides scalable open-source MQTT client implementations for various languages. The following examples use the Eclipse Paho Java library for the MQTT client.
Obtaining the Library
With Maven: pom.xml
......
<repositories>
<repository>
<id>Eclipse Paho Repo</id>
<url>https://repo.eclipse.org/content/repositories/paho-releases/</url>
</repository>
</repositories>
....
<dependencies>
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.0.2</version>
</dependency>
</dependencies>
With Gradle: build.gradle
repositories {
maven { url ‘https://repo.eclipse.org/content/
repositories/paho-releases/’ } }
dependencies {
compile( [group: ‘org.eclipse.paho’, name: ‘org.
eclipse.paho.client.mqttv3’, version: ‘1.0.2’] ) }
Publish a Message
Publishing messages is straightforward. After connecting, publishing is a one-liner with the publish() method.
MqttClient mqttClient = new MqttClient(
“tcp://broker.mqttdashboard.com:1883”, //1
“refcard-client”); //2
mqttClient.connect();
mqttClient.publish(
“topic”, //3
“message”.getBytes(), //4
0, //5
false); //6
mqttClient.disconnect();
The server URI
The MQTT client ID
The MQTT topic
The payload as byte array
The QoS Level
Retained Flag
Subscribe to Topics
In order to subscribe to topics, an MqttCallback must be implemented. This callback is triggered every time an event (like messageArrived) occurs. This callback must be implemented before connecting to the broker.
mqttClient.setCallback(new MqttCallback() { //1
@Override
public void connectionLost(Throwable throwable) {
//Called when connection is lost.
}
@Override
public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
System.out.println("Topic: " + topic);
System.out.println(new String(mqttMessage. getPayload()));
System.out.println("QoS: " + mqttMessage. getQos());
System.out.println("Retained: " + mqttMessage. isRetained());
}
@Override
public void deliveryComplete(final IMqttDeliveryToken iMqttDeliveryToken) {
//When message delivery was complete
}
});
mqttClient.connect();
mqttClient.subscribe("my/topic", 2); //2
1. Implement the MqttCallback in order to process messages which match the subscription
2. Subscribe to a topic with Quality of Service level 2
Connecting With Additional Options
For more sophisticated MQTT applications, there are additional options for establishing a connection to the broker available.
MqttConnectOptions options = new MqttConnectOptions();
options.setCleanSession(true); //1
options.setKeepAliveInterval(180); //2
options.setMqttVersion(MqttConnectOptions.MQTT_ VERSION_3_1_1); //3
options.setUserName("username"); //4
options.setPassword("mypw".toCharArray()); //5
options.setWill("will/topic", //6
"message".getBytes(), //7
1, //8
true);//9
mqttClient.connect(options);
If a clean or persistent session should be used
Interval in seconds for heartbeats
MQTT version (3.1 or 3.1.1)
- Username for authentication
- Password for authentication
- Topic for Las tWill and Testament
- Last Will and Testament message
- Last Will and Testament QoS
- Last Will and Testament Retained Flag
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}