Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Paho » Publish message from callback messageArrived()(How to publish a message within the callback method messageArrived without blocking)
Publish message from callback messageArrived() [message #1729610] Sat, 16 April 2016 00:02 Go to next message
Dominik Pichler is currently offline Dominik PichlerFriend
Messages: 1
Registered: April 2016
Junior Member
Hey guys,

I'm new to MQTT and have the following question. Why does client.publish(...) block if it is invoked inside the MqttCallback-method messageArrived(...)? (The complete program is at the end). How can I avoid this?

The first two messages

client.publish("my/topic", ("message1 works").getBytes(), 2, false);
client.publish("my/topic", ("message2 works").getBytes(), 2, false);

What am I doing wrong?

The documentation to this method says:

It is possible to send a new message within an implementation of this callback (for example, a response to this message), but the implementation must not disconnect the client, as it will be impossible to send an acknowledgment for the message being processed, and a deadlock will occur.
But how?

Here is the complete Code:

import org.eclipse.paho.client.mqttv3.*;

class SimpleClient implements MqttCallback{

    private static final String MQTT_BROKER = "tcp://localhost:1883";
    private MqttClient client;


        try {
            client = new MqttClient(MQTT_BROKER, "myID");


            client.publish("my/topic", ("message1 works").getBytes(), 2, false);
            client.publish("my/topic", ("message2 works").getBytes(), 2, false);



        } catch (MqttException e) {

    public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {

        System.out.println("Here I am");

        client.publish("some/other/topic", ("message").getBytes(), 2, false);

        System.out.println("I don't reach this point!");


    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
        // not needed

    public void connectionLost(Throwable throwable) {
        // not needed
Re: Publish message from callback messageArrived() [message #1731530 is a reply to message #1729610] Fri, 06 May 2016 14:03 Go to previous message
James Sutton is currently offline James SuttonFriend
Messages: 71
Registered: July 2015
You're subscribing after you've published your messages, so by the time the subscription is complete, the published messages have been and gone.
In the screenshot below, I'm using your code, but I'm manually publishing after the subscription has been made, you can see that the code outputs to System.out and also publishes the message to some/other/topic.
Make sure you subscribe first! Smile
Hope that helps!
  • Attachment: sub_test.png
    (Size: 59.05KB, Downloaded 1855 times)
Previous Topic:Concurrent subscribe, unsubscribe and publish operations using same MqttClient instance
Next Topic:Paho mqtt connection issue
Goto Forum:

Current Time: Sun Oct 25 20:28:15 GMT 2020

Powered by FUDForum. Page generated in 0.01986 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top