Subscription callback works in unit test but not in Anroid app (newb)

I’m a newb building an Android app using HiveMQ.to communicate with an IoT broker.

My stand-alone unit test works great: I can subscribe to a topic, publish a message, and see the response come into my subscription callback:

        mqttClient.subscribeWith()
            .topicFilter(this.clientResponseTopic)
            .callback(published -> {
                Log.d(TAG, "subscribe: callback: enter");
                String string = new String(published.getPayloadAsBytes(), StandardCharsets.UTF_8);
                Log.d(TAG, "subscribe: callback: topic=" + published.getTopic() + "\n" + string);
            })
            .send()
            .whenComplete((ack, exception) -> {
              ...
            });

And the unit test logs show the output:

subscribe: callback: topic=/app/2208401-0744fb78a23042d1fada1647c1a8576e/subscribe

However, when I run my App, I can see the subscription request sent successfully, but I never ever hear any messages in my callback. I’ve tested both in an emulator, and on my personal device.

I’ve read the HiveMQ Andriod Guide, and I’ve checked all of that twice.

I feel like something is either blocking responses from coming in, or Android is killing whatever it is HiveMQ stands up to listen for responses.

If someone can suggest what I might be doing wrong, I’d appreciate it. If not, I’d also appreciate any HiveMQ/Android debug/diagnostic/triage tips.

I’ve added ‘org.slf4j:slf4j-android:1.7.30’ to my Gradle project in hopes of getting HiveMQ’s logging to show up, but it seems that is flawed.

Hi @Trinition ,

To see if the issue is on the broker side or on the client side, I would install HiveMQ broker locally, in order to have full control over the traffic and to be able to see all communication logs on the broker side.

To install a HiveMQ broker locally, you can either download an open source HiveMQ - Community Edition or download a HiveMQ Enterprise Edition (which you can run for 5h without a license, in Trial mode). It does not matter for the test, which edition you install.

To see all the MQTT messages in the HiveMQ broker log (e.g. CONNECT, CONNACK, SUBSCRIBE, SUBACK etc.) you will need to install the open source HiveMQ MQTT Message Log Extension.

Testing your MQTT client against the HiveMQ broker with MQTT Message Log Extension enabled will let you see which MQTT packets reach the broker and which are sent by the broker to the client. This way you can see if the issue is in the client’s code or if it is a communication issue with the broker.

Without running a broker locally, it should be possible to sniff the traffic between your MQTT client and the broker with Wireshark, however, it is rather hard to decode the traffic without knowing what exactly you are looking for.

I hope this helps.

Kind regards,
Dasha from HiveMQ Team

Thank you for your advice, Daria_H. Before your reply, I made a key discovery: my unit test is skipping one step that my app is performing, and it is that extra step that is failing.

My unit test flow is:

  1. Connect to Broker
  2. Publish a Set message

My app flow is:

  1. Connect to Broker
  2. Publish a Status Get message and wait for response
  3. Publish a Set message

App step #2 is what was never getting a response, so it never got to #3.

So it turns out my problem was of my own making.

Now my task is find out why another app is able to sent a Get Status message and get a response, but mine isn’t.

I’m investigating ways of sniffing the traffic from the other app, but it’s tricky since MQTT is not HTTP, and the client-broker connection is TLS.

Hey, @Trinition ,

When you say:

What kind of “response” is your MQTT client (app) waiting for?

  • If that is a message from some other client, then your client must subscribe to the corresponding topic first. You can find more information on the Request-Response pattern in MQTT 5 in MQTT Request-Response Pattern – MQTT 5 Essentials Part 9.
  • If your app is sending a PUBLISH message and waiting for a PUBACK message, you need to use QoS 1 for the PUBLISH message.
  • If your app is using QoS 2, then it will receive a PUBREC message instead of PUBACK.
  • If your app is using QoS 1, then it will not receive any acknowledgement from the broker. You can find more info on QoS and message flow in What is MQTT Quality of Service (QoS) 0,1, & 2? – MQTT Essentials: Part 6

I hope it helps,
Best regards,
Dasha from HiveMQ Team