0

The problem is, when the client sends it's first message after getting a message, it can no longer receive and reply with a message.

Here is the code:

public void demo() {
    try {
        client = new MqttClient("tcp://broker:1883", "Sending");
        client.connect();
        client.setCallback(this);
        client.subscribe("receive");
    } catch (MqttException e) {}
}


@Override
public void messageArrived(String topic, MqttMessage message)
        throws Exception {      
    message.setPayload("I'm replying".getBytes());
    client.publish("publish", message);
}

I have something similar in Android development and it works as a charm. Paho 3-1.0.2

Yan
  • 582
  • 2
  • 6
  • 22

1 Answers1

0

You should not publish new messages from with in the massageArrived callback.

Use something like a AsyncTask to do the publish from a separate thread

Edit, sorry, not 100% awake, miss read the on Android bit. rest still holds, just need to use a Excutor and Runnable rather than AsyncTask for normal Java

hardillb
  • 54,545
  • 11
  • 67
  • 105