0

I'm writing a mqtt app in C++ with Paho. I'm using Paho.mqtt C library that can be found here.

When I receive a message in the callback function, it calls another function to print the message. I want to be able to loop that function until a new message arrives.

bool start = false;
void loopMessage(std::string message){
   start = true;
   while(start){
       std::out << "message: " << message;
   }
}
int messageArrivedCallback(mqtt message){
   start = false;
   loopMessage(message);
}

The code above is not working; I can receive one message and run the while loop in the loopMessage function. After that the client disconnects. I think this is because it is in blocking mode and the broker disconnects the client after the keep alive interval (I'm using MQTTClient). If I quickly send a new message before disconnection, the client doesn't receive it.

I tried to use the asynchronus client version (MQTTAsync) but it is giving me the same problem.

Any suggestions on how to do this?

Zimano
  • 1,870
  • 2
  • 23
  • 41
Janoshh
  • 19
  • 2
  • 9

1 Answers1

0

The Paho client will start a thread to handle the network loop and collect new messages so just remove the loopMessage function and print the message in the messageArrivedCallback.

You should not make blocking calls (which is what this infinite loop does) in the messageArrivedCallback because it has to return to allow the client to continue to receive messages.

If you want to print the message content over and over agian until a new message arrives then you need to do it on a separate thread.

hardillb
  • 54,545
  • 11
  • 67
  • 105
  • Hi! If there is a while loop in the callback function i think the callback can't receive new messages, or am i wrong? – Janoshh Mar 29 '17 at 09:28
  • @Janoshh: That doesn't make sense. Of course the callback function can receive new messages, but it can only receive one message at a time. It can't receive a second message until it's done with the first. And the callback is only done with a message when it returns. – MSalters Mar 29 '17 at 09:31
  • @hardillb: Yes i meant: if the there is a while loop in the callback, the callback is never returning so it can't receive a new message. allright, i had the same idea but threading is mostly a bad solution and i was thinking there might be a better one – Janoshh Mar 29 '17 at 09:32
  • Threading is not bad, it's just not easy. It's also the only way to do what you want – hardillb Mar 29 '17 at 10:22
  • So the asynchronus client isn't any better than the synchronus client for my example? – Janoshh Mar 29 '17 at 11:09
  • No, the async client doesn't block when connecting and publishing, it still expects you not to block on messages delivery – hardillb Mar 29 '17 at 12:53
  • ahhhhhh, this is a useful thread no pun intended. Threading was the recommended way I do this but thought Id try and get away with just the while loop but it doesnt work. If the while loop for a topic is not finished it blocks the other messages for that topic as stated above. – Mike Sandstrom Mar 24 '20 at 22:40
  • could celery have been a solution to OP's problem? – Mike Sandstrom Mar 24 '20 at 23:56