In the last few years we used Vert.x to prototype and develop several IoT related projects. Most of the projects use MQTT for lightweight messaging between the connected devices (or things ;)) and the applications. The messaging is handled by an external broker liker Mosquitto. Instead of using an external broker it’s now possible to connect MQTT enabled devices directly to Vert.x using the Vert.x MQTT server project. Although the project is still in Tech Preview we’ll show you how use it to create a MQTT server within Vert.x. At first we add the snapshot repository to our build.gradle file.

repositories {
    maven {
        url ""

We also add our dependencies including the Vert.x MQTT server project.

dependencies {

    // Vertx dependencies
    compile "io.vertx:vertx-core:${vertx.version}"
    compile "io.vertx:vertx-mqtt-server:3.4.0-SNAPSHOT"

    // Logging dependencies
    compile 'org.slf4j:slf4j-api:1.7.18'
    runtime 'ch.qos.logback:logback-classic:1.1.6'

Using the Vert.x MQTT server project we are able to create a MQTT server from within a Vert.x Verticle.

package com.jdriven;

import ...

public class VertxMqttDemoVerticle extends AbstractVerticle {

    private static final Logger log = LoggerFactory.getLogger(MqttServerVerticle.class);

    private MqttServer server;

    public void start() throws Exception {

        // Configure the MQTT Server
        MqttServerOptions options = new MqttServerOptions();

        server = MqttServer.create(this.vertx, options);

        // Register a handler for incoming connections
        server.endpointHandler(endpoint -> {
            log.debug("Client connected: {0}",

            // Register a handler for message published on the endpoint
            endpoint.publishHandler(message -> {

                // Log the contents of the message
      "Received message at {0}, payload: {1}, qos: {2}",

            // Acknowledge the connection to the client
            endpoint.writeConnack(CONNECTION_ACCEPTED, false);

        // Start the MQTT Server
        this.server.listen(result -> {
            if (result.succeeded()) {
      "MQTT server started on port: {0}", result.result().actualPort());
            } else {
                log.error("MQTT server failed to start: {0}", result.cause().getMessage(), result.cause());

The "complete" code of this example can be found here and is heavily based on: