2

I'm using MQTT with paho to receive and publish messages on android.

I have the following code for my MQTT initialization.

private void initializeMQTT(){
    try{
        mqttClient = new MqttClient(
                "tcp://broker.hivemq.com:1883",
                MqttClient.generateClientId(),
                new MemoryPersistence()
        );

        mqttClient.connect();
        mqttConnected = mqttClient.isConnected();
        mqttClient.subscribe("testtopic/listen",1);

        mqttClient.setCallback(new MqttCallback() {
            @Override
            public void connectionLost(Throwable cause) { //Called when the client lost the connection to the broker
            }

            @Override
            public void messageArrived(String topic, MqttMessage message) throws Exception {
                mqttPayload = topic + ": " + Arrays.toString(message.getPayload());
                mqttAnswer.setPayload(mqttPayload.getBytes());
                mqttClient.publish("testtopic/publish",mqttAnswer);
            }

            @Override
            public void deliveryComplete(IMqttDeliveryToken token) {//Called when a outgoing publish is complete
                messageInfoTest = "message was sent";
            }
        });
    }
    catch(MqttException e){

    }

}

I just want to take the message I receive and send it back somewhere else as a test.

What happens right now is I receive the first time I publish on the testtopic/receive topic. I do not seem to be publishing anything back. And if I try to send another message to testtopic/receive it is never receive on my android.

Anybody has an idea of what I'm currently missing?

Thanks!

lhbortho
  • 167
  • 2
  • 16
  • How is this different from your last qustion? http://stackoverflow.com/questions/41882501/mqtt-android-app-publish-in-callback-function-for-messagearrived – hardillb Jan 30 '17 at 16:55
  • It's not. I don't know how question priority works on here. I figured it might get buried as I edited it to add code a day after posting it and figured I would re-post in the off chance it would get the attention of someone who might have passed over the question when it had no code at first. – lhbortho Jan 30 '17 at 17:05
  • Do not post the same question multiple times, they will just get closed as duplicates – hardillb Jan 30 '17 at 17:06
  • Possible duplicate of [MQTT android app publish in callback function for messageArrived](http://stackoverflow.com/questions/41882501/mqtt-android-app-publish-in-callback-function-for-messagearrived) – hardillb Jan 30 '17 at 17:06
  • Thanks I will keep that in mind next time, I deleted the other post. – lhbortho Jan 30 '17 at 17:07
  • I'm having the exact same problem. When I subscirbe to my topic using another client than android I'm getting all published messages. But android client (paho) is only getting the first message. – uylmz Mar 10 '17 at 14:18
  • Never found what maybe causing this, I'm still using 2 clients, one to publish and one to subscribe and don't have a problem this way. – lhbortho Mar 12 '17 at 23:37

1 Answers1

0

I just had the same problem, after debugging PAHO found that client.publish must be called from other thread than the thread calling messageArrived(...) callback. client.publish(...) can't be called from messageArrived(...) callback code, because it causes deadlock.

marioc64
  • 381
  • 3
  • 12
  • Very interesting, I've simply lived with the fact that I had to have 2 clients. If I ever go back to this code I'll optimize it that way. Thanks for the answer! – lhbortho Aug 17 '17 at 14:45
  • Try to comment mqttClient.publish(), and you will see that every message is arriving. On Android you can use AsyncTask. – marioc64 Aug 18 '17 at 14:57