HiveMQ

Subscribe to two different topics

How to subscribe to two different topics using MQTT java client?.
//Example code with single topic sub: SUCCESS
mqtt3AsyncClient.subscribeWith().topicFilter(“tenant1/topic”)
.qos(MqttQos.AT_LEAST_ONCE).callback(e -> {}).send();

//Subscribing to two topics : FAILED
mqtt3AsyncClient.subscribeWith().topicFilter(“tenant1/topic”).topicFilter(“tenant2/topic”)
.qos(MqttQos.AT_LEAST_ONCE).callback(e -> {}).send();

Hi @dileepmandapam,

in your example with two topics you are overriding “tenant1/topic” with “tenant2/topic”.

One way to subscribe to two topics (or more) is:

mqtt3AsyncClient.subscribeWith()
.addSubscription().topicFilter(“tenant1/topic”).qos(MqttQos.AT_LEAST_ONCE).applySubscription()
.addSubscription().topicFilter(“tenant2/topic”).qos(MqttQos.AT_LEAST_ONCE).applySubscription()
.callback(e -> {})
.send();

Greetings,
Michael from the HiveMQ team