Introducing Vert.x MQTT client

In this ar­ti­cle, we will see how to set up the new Vert.x MQTT client. Ac­tu­ally, I have a real ex­am­ple so you can try it quickly.

If you are using Maven or Gra­dle, add the fol­low­ing de­pen­dency to the de­pen­den­cies sec­tion of your project de­scrip­tor to ac­cess the Vert.x MQTT client:

  • Maven (in your pom.xml):
<dependency>
    <groupId>io.vertx</groupId>
    <artifactId>vertx-mqtt</artifactId>
    <version>3.5.0.Beta1</version>
</dependency>
  • Gra­dle (in your build.gra­dle file):
dependencies {
  compile 'io.vertx:vertx-mqtt:3.5.0.Beta1'
}

Now that you’ve set up your project, you can cre­ate a sim­ple ap­pli­ca­tion which will re­ceive all mes­sages from all bro­ker chan­nels:


import io.vertx.core.AbstractVerticle;
import io.vertx.mqtt.MqttClient;
import io.vertx.mqtt.MqttClientOptions;

import java.io.UnsupportedEncodingException;

public class MainVerticle extends AbstractVerticle {

  @Override
  public void start() {
     MqttClientOptions options = new MqttClientOptions();
      // specify broker host
      options.setHost("iot.eclipse.org");
      // specify max size of message in bytes
      options.setMaxMessageSize(100_000_000);

    MqttClient client = MqttClient.create(vertx, options);

    client.publishHandler(s -> {
      try {
        String message = new String(s.payload().getBytes(), "UTF-8");
        System.out.println(String.format("Receive message with content: \"%s\" from topic \"%s\"", message, s.topicName()));
      } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
      }
    });

    client.connect(s -> {
      // subscribe to all subtopics
      client.subscribe("#", 0);
    });
  }
}

The pub­lish­Han­dler is the han­dler called each time the bro­ker, lo­cated at iot.eclipse.org:1883, sends a mes­sage to you, from the top­ics you are sub­scrib­ing for.

But only pro­vid­ing a han­dler is not enough, you should also con­nect to the bro­ker and sub­scribe to some top­ics. For this rea­son, you should use the con­nect method and then call sub­scribe when the con­nec­tion es­tab­lished.

To de­ploy this ver­ti­cle from an ap­pli­ca­tion you should have in your main method some­thing like that:

Vertx vertx = Vertx.vertx();
vertx.deployVerticle(MainVerticle.class.getCanonicalName());

When you have com­pleted all steps cor­rectly the re­sult should look like that:

As the al­ter­na­tive and rec­om­mended way to boot­strap Vert.x ap­pli­ca­tions you can use vertx-​maven-starter or vertx-​gradle-starter. For com­plet­ing this guide I have used the first one. The final source code avail­able here. If you would like to learn more about Vert.x MQTT client API then check out the full doc­u­men­ta­tion and more ex­am­ples.

Thank you for read­ing!

Cheers!

Next post

An Eclipse Vert.x Gradle Plugin

The new Vert.x Gradle plugin offers an opinionated plugin for building Vert.x applications with Gradle.

Read more
Previous post

Vert.x 3.5.0.Beta1

It's summer time and we have just released Vert.x 3.5.0.Beta1!

Read more
Related posts

TCP Client using Eclipse Vert.x, Kotlin and Gradle build

In this blog post, I demonstrate how to write a very simple TCP client that keeps a connection open to a custom-written server in cloud.

Read more

Real-time bidding with Websockets and Vert.x

The expectations of users for interactivity with web applications have changed over the past few years. Users during bidding in auction no longer want to press the refresh button.

Read more

Checklist for Migrating from Vert.x 2.1.x to Vert.x 3 - Part One

So while upgrading our application, I thought I should note down all the changes that we had to do in the process. Since Vert.x 3 is a major upgrade from the previous version, with so many changes.

Read more