Link Search Menu Expand Document Edit GitHub

Quick Start

The following contains all the steps necessary to integrate the HiveMQ MQTT Client library into a project, connect to a broker, then subscribe to a topic and publish messages to a topic using the MQTT 3 asynchronous API flavour.

For a more detailed description of how to use the client, its API flavours and the more powerful features, see the other parts of the documentation.

Adding the HiveMQ MQTT Client library to your project

In order to include the HiveMQ MQTT Client library in your gradle project, add the following dependency:

dependencies {
    implementation 'com.hivemq:hivemq-mqtt-client:1.3.3'

Similarly, for Maven, include the following dependency:


Creating the client

Using the builder accessible from the com.hivemq.client.mqtt.MqttClient class you are able to configure and create an MQTT client, which you can then use to connect to the broker, subscribe to topics and publish messages. The following example shows connecting to a local MQTT broker using SSL and the MQTT 3.1.1 protocol:

Mqtt3AsyncClient client = MqttClient.builder()

Connecting to the broker

Once you have created your client, the next step is to actually connect to the broker. The following example shows connecting using a username and password:

        .whenComplete((connAck, throwable) -> {
            if (throwable != null) {
                // handle failure
            } else {
                // setup subscribes or start publishing

Subscribing to a topic

Given a client which has successfully connected to a broker, you can setup your subscriptions:

        .callback(publish -> {
            // Process the received message
        .whenComplete((subAck, throwable) -> {
            if (throwable != null) {
                // Handle failure to subscribe
            } else {
                // Handle successful subscription, e.g. logging or incrementing a metric

Publishing to a topic

Similar to subscribing, once you have a connected client, you can also publish messages to a topic:

        .payload("hello world".getBytes())
        .whenComplete((publish, throwable) -> {
            if (throwable != null) {
                // handle failure to publish
            } else {
                // handle successful publish, e.g. logging or incrementing a metric

Disconnecting the client

Once you are finished using the client, for example you’ve sent all the messages you wanted to send or the application is shutting down, you simply call disconnect() on the client. As with the other messages, you will receive a CompletableFuture where you can react to success or failure of the disconnect.