Shaded version

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.
To use the shaded version just append -shaded to the artifact name.

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.

Disconnect

disconnect(), disconnectWith() and disconnect(Mqtt5Disconnect) method calls are analog to the Blocking API but
return CompletableFuture.

Reauth (only MQTT 5)

reauth() method call is analog to the Blocking API but return CompletableFuture.

Reactive API

Builder method: buildRx()

Switch method: client.toRx()

Examples

Subscribe example

Mqtt5RxClient client =Mqtt5Client.builder()
.identifier(UUID.randomUUID().toString())
.serverHost("broker.hivemq.com")
.buildRx();
// As we use the reactive API, the following line does not connect yet, but returns a reactive type.// e.g. Single is something like a lazy and reusable future. Think of it as a source for the ConnAck message.Single<Mqtt5ConnAck> connAckSingle = client.connect();
// Same here: the following line does not subscribe yet, but returns a reactive type.// FlowableWithSingle is a combination of the single SubAck message and a Flowable of Publish messages.// A Flowable is an asynchronous stream that enables backpressure from the application over the client to the broker.FlowableWithSingle<Mqtt5Publish, Mqtt5SubAck> subAckAndMatchingPublishes = client.subscribeStreamWith()
.topicFilter("a/b/c").qos(MqttQos.AT_LEAST_ONCE)
.addSubscription().topicFilter("a/b/c/d").qos(MqttQos.EXACTLY_ONCE).applySubscription()
.applySubscribe();
// The reactive types offer many operators that will not be covered here.// Here we register callbacks to print messages when we received the CONNACK, SUBACK and matching PUBLISH messages.Completable connectScenario = connAckSingle
.doOnSuccess(connAck ->System.out.println("Connected, "+ connAck.getReasonCode()))
.doOnError(throwable ->System.out.println("Connection failed, "+ throwable.getMessage()))
.ignoreElement();
Completable subscribeScenario = subAckAndMatchingPublishes
.doOnSingle(subAck ->System.out.println("Subscribed, "+ subAck.getReasonCodes()))
.doOnNext(publish ->System.out.println(
"Received publish"+", topic: "+ publish.getTopic() +", QoS: "+ publish.getQos() +", payload: "+newString(publish.getPayloadAsBytes())))
.ignoreElements();
// Reactive types can be easily and flexibly combined
connectScenario.andThen(subscribeScenario).blockingAwait();

Publish example

Mqtt5RxClient client =Mqtt5Client.builder()
.identifier(UUID.randomUUID().toString())
.serverHost("broker.hivemq.com")
.buildRx();
// As we use the reactive API, the following line does not connect yet, but returns a reactive type.Completable connectScenario = client.connect()
.doOnSuccess(connAck ->System.out.println("Connected, "+ connAck.getReasonCode()))
.doOnError(throwable ->System.out.println("Connection failed, "+ throwable.getMessage()))
.ignoreElement();
// Fake a stream of Publish messages with an incrementing number in the payloadFlowable<Mqtt5Publish> messagesToPublish =Flowable.range(0, 10_000)
.map(i ->Mqtt5Publish.builder()
.topic("a/b/c")
.qos(MqttQos.AT_LEAST_ONCE)
.payload(("test "+ i).getBytes())
.build())
// Emit 1 message only every 100 milliseconds
.zipWith(Flowable.interval(100, TimeUnit.MILLISECONDS), (publish, i) -> publish);
// As we use the reactive API, the following line does not publish yet, but returns a reactive type.Completable publishScenario = client.publish(messagesToPublish)
.doOnNext(publishResult ->System.out.println(
"Publish acknowledged: "+newString(publishResult.getPublish().getPayloadAsBytes())))
.ignoreElements();
// As we use the reactive API, the following line does not disconnect yet, but returns a reactive type.Completable disconnectScenario = client.disconnect().doOnComplete(() ->System.out.println("Disconnected"));
// Reactive types can be easily and flexibly combined
connectScenario.andThen(publishScenario).andThen(disconnectScenario).blockingAwait();

Connect

connect(), connectWith() and connect(Mqtt3/5Connect) method calls are analog to the Async and Blocking API but
return Single<ConnAck>.