HiveMQ MQTT Client is an MQTT 5.0 and MQTT 3.1.1 compatible and feature-rich high-performance Java client library with different API flavours and backpressure s
HiveMQ MQTT Client is an MQTT 5.0 and MQTT 3.1.1 compatible and feature-rich high-performance Java client library with different API flavours and backpressure s
MQTT 5.0 and 3.1.1 compatible and feature-rich high-performance Java client library with different API flavours and backpressure support.
[
](https://github.com/bmwcarit) [
](https://github.com/bmwcarit/joynr) [
](https://github.com/eclipse/ditto) [
](https://github.com/OSGP/open-smart-grid-platform) [
](https://github.com/EXXETA/correomqtt) [
](https://github.com/SmartsquareGmbH/mqtt-starter) [
](https://github.com/leftshiftone/gaia-sdk)
If you use the HiveMQ MQTT Client in a project that is not listed here, feel free to open an issue or pull request.
Java 8 or higher is required.
If you use Gradle, just include the following inside your build.gradle(.kts) file.
dependencies {
implementation("com.hivemq:hivemq-mqtt-client:1.4.0")
}For optional features you can choose to include additional modules:
dependencies {
implementation(platform("com.hivemq:hivemq-mqtt-client-websocket:1.4.0"))
implementation(platform("com.hivemq:hivemq-mqtt-client-proxy:1.4.0"))
implementation(platform("com.hivemq:hivemq-mqtt-client-epoll:1.4.0"))
implementation("com.hivemq:hivemq-mqtt-client-reactor:1.4.0")
}If you use Maven, just include the following inside your pom.xml file.
...
com.hivemq
hivemq-mqtt-client
1.4.0
...
NOTE: You have to set the compiler version to 1.8 or higher.
...
1.8
1.8
...
For optional features you can choose to include additional modules:
...
com.hivemq
hivemq-mqtt-client-websocket
1.4.0
pom
com.hivemq
hivemq-mqtt-client-proxy
1.4.0
pom
com.hivemq
hivemq-mqtt-client-epoll
1.4.0
pom
com.hivemq
hivemq-mqtt-client-reactor
1.4.0
...
If you are experiencing problems with transitive dependencies, you can try the shaded version.
This version packs the transitive dependencies which are only used internal under a different package name.
The shaded version includes the websocket, proxy and epoll modules.
To use the shaded version just append -shaded to the artifact name.
dependencies {
implementation("com.hivemq:hivemq-mqtt-client-shaded:1.4.0")
}
...
com.hivemq
hivemq-mqtt-client-shaded
1.4.0
...
Snapshots can be obtained using JitPack.
……Change the artifact name to hivemq-mqtt-client-shaded to get snapshots of the shaded version.
JitPack works for all branches and also specific commits.
Just specify -SNAPSHOT or the first 10 digits of the commit id in the version.
internal packages must not be used directly.Base classes: Mqtt3Client, Mqtt5Client
Mqtt5Client client = MqttClient.builder()
.identifier(UUID.randomUUID().toString())
.serverHost("broker.hivemq.com")
.useMqttVersion5()
.build();
Mqtt3Client client = MqttClient.builder()...useMqttVersion3().build();Or if the version is known upfront:
Mqtt5Client client = Mqtt5Client.builder()...build();
Mqtt3Client client = Mqtt3Client.builder()...build();For each API style exists a specific build...() method.
Each API style has its own interface to separate them clearly. At any time it is possible to switch the API style.
buildBlocking()client.toBlocking()final Mqtt5BlockingClient client = Mqtt5Client.builder()
.identifier(UUID.randomUUID().toString())
.serverHost("broker.hivemq.com")
.buildBlocking();
client.connect();
try (final Mqtt5Publishes publishes = client.publishes(MqttGlobalPublishFilter.ALL)) {
client.subscribeWith().topicFilter("test/topic").qos(MqttQos.AT_LEAST_ONCE).send();
publishes.receive(1, TimeUnit.SECONDS).ifPresent(System.out::println);
publishes.receive(100, TimeUnit.MILLISECONDS).ifPresent(System.out::println);
} finally {
client.disconnect();
}Mqtt5BlockingClient client = Mqtt5Client.builder()
.identifier(UUID.randomUUID().toString())
.serverHost("broker.hivemq.com")
.buildBlocking();
client.connect();
client.publishWith().topic("test/topic").qos(MqttQos.AT_LEAST_ONCE).payload("1".getBytes()).send();
client.disconnect();client.connect();Or with customized properties of the Connect message:
client.connectWith().keepAlive(10).send();Or with pre-built Connect message:
Mqtt5Connect connectMessage = Mqtt5Connect.builder().keepAlive(10).build();
client.connect(connectMessage);client.publishWith()
.topic("test/topic")
.qos(MqttQos.AT_LEAST_ONCE)
.payload("payload".getBytes())
.send();Or with pre-built Publish message:
Mqtt5Publish publishMessage = Mqtt5Publish.builder()
.topic("test/topic")
.qos(MqttQos.AT_LEAST_ONCE)
.payload("payload".getBytes())
.build();
client.publish(publishMessage);client.subscribeWith().topicFilter("test/topic").qos(MqttQos.EXACTLY_ONCE).send();Or with pre-built Subscribe message:
Mqtt5Subscribe subscribeMessage = Mqtt5Subscribe.builder()
.topicFilter("test/topic")
.qos(MqttQos.EXACTLY_ONCE)
.build();
client.subscribe(subscribeMessage);client.unsubscribeWith().topicFilter("test/topic").send();Or with pre-built Unsubscribe message:
Mqtt5Unsubscribe unsubscribeMessage = Mqtt5Unsubscribe.builder().topicFilter("test/topic").build();
client.unsubscribe(unsubscribeMessage);try (final Mqtt5Publishes publishes = client.publishes(MqttGlobalPublishFilter.ALL)) {
Mqtt5Publish publishMessage = publishes.receive();
// or with timeout
Optional publishMessage = publishes.receive(10, TimeUnit.SECONDS);
// or without blocking
Optional publishMessage = publishes.receiveNow();
}publishes must be called before subscribe to ensure no message is lost.
It can be called before connect to receive messages of a previous session.
client.disconnect();Or with customized properties of the DISCONNECT message (only MQTT 5):
client.disconnectWith().reasonString("test").send();Or with pre-built Disconnect message (only MQTT 5):
Mqtt5Disconnect disconnectMessage = Mqtt5Disconnect.builder().reasonString("test").build();
client.disconnect(disconnectMessage);client.reauth();buildAsync()client.toAsync()Mqtt5BlockingClient client = Mqtt5Client.builder()
.identifier(UUID.randomUUID().toString())
.serverHost("broker.hivemq.com")
.buildBlocking();
client.connect();
client.toAsync().subscribeWith()
.topicFilter("test/topic")
.qos(MqttQos.AT_LEAST_ONCE)
.callback(System.out::println)
.send();Mqtt5AsyncClient client = Mqtt5Client.builder()
.identifier(UUID.randomUUID().toString())
.serverHost("broker.hivemq.com")
.buildAsync();
client.connect()
.thenCompose(connAck -> client.publishWith().topic("test/topic").payload("1".getBytes()).send())
.thenCompose(publishResult -> client.disconnect());connect(), connectWith() and connect(Mqtt3/5Connect) method calls are analog to the Blocking API but return
CompletableFuture.
publishWith() and publish(Mqtt3/5Publish) method calls are analog to the Blocking API but return
CompletableFuture.
subscribeWith() and subscribe(Mqtt3/5Subscribe) method calls are analog to the Blocking API but return
CompletableFuture.
Additionally messages can be consumed per subscribe:
client.subscribeWith()
.topicFilter("test/topic")
.qos(MqttQos.EXACTLY_ONCE)
.callback(System.out::println)
.executor(executor) // optional
.send();Or with pre-built Subscribe message:
Mqtt5Subscribe subscribeMessage = Mqtt5Subscribe.builder()
.topicFilter("test/topNo open issues yet, or sync has not completed.