qtmqtt connection issue
-
Hello,
there is one thing I can't really understand using qtmqtt. I'm creating a console application to handle mqtt based communication. There is a method (technically a slot) in my class performing the following
void master::prep() { mqttcli = new QMqttClient(this); mqttcli->setHostname(QString("192.168.1.4")); mqttcli->setPort(1883); mqttcli->connectToHost(); // (*) ending method here results in connection mqttcli->subscribe("avu1"), 0); }
The above snipped simply wont work since the connection doesn't establish and of course no subscription is possible. If the method would be finished in (*), however, the connection would be successfully established. But then the subscription part should be moved to another slot and called after
prep()
.I'm curious whats is the origin of such behavior. Why
connectToHost()
needs to be followed by the end of the method to be successful?Thanks,
Pumice -
Hi and welcome to devnet,
I haven't used the module yet but
connectToHost
is not a blocking method so currently your likely trying to subscribe to a topic while the client is not yet connected to the broker. -
Hi and thank you for the welcome.
Well, thats what makes me curious. From various observations: until the method is not finished, the connection won’t be established. The latter make the program unusable to subscribe, clearly.
Why would behaviour of the function depend on the wrapping function?
Thanks,
Pumice -
Hi and thank you for the welcome.
Well, thats what makes me curious. From various observations: until the method is not finished, the connection won’t be established. The latter make the program unusable to subscribe, clearly.
Why would behaviour of the function depend on the wrapping function?
Thanks,
Pumice@Pumice Connect a slot to https://doc.qt.io/QtMQTT/qmqttclient.html#connected and in this slot call
mqttcli->subscribe("avu1"), 0);
As @SGaist said connectToHost() simply initiates the connection process. This means - when it returns the connection is not yet established, so calling subscribe() just afterwards is not going to work as there is no connection yet. This is how asynchronous programming works.
"Why would behaviour of the function depend on the wrapping function?" - it doesn't, you simply call subscribe() too early. -
@jsulm, thanks for your post. This is something I understand. Consider the following.
void master::prep() { ... mqttcli->connectToHost(); connect(mqttcli, &QMqttClient::connected, this, [this] { mqttcli->subscribe(QString("avu1"), 0); }); QThread::msleep(N*1000); //^^^^^^^^^^^^^^^^^^^^^ }
What is still not clear for me, why does it take N seconds to establish the connection? I mean – that's clear
connectToHost()
needs some time. But what I observe is it also needs to reach the end ofprep()
. What isconnectToHost()
thinking? Thanks for calling me, Sir, but I will not connect to host until the method that called me ends.I assume that I may have severe lack of knowledge in certain areas of programming, but that's the most surprising behavior I met so far. And I can't even know where to start digging. That's why I continue the thread, event though the code works.
Thanks for your time,
Pumice -
@jsulm, thanks for your post. This is something I understand. Consider the following.
void master::prep() { ... mqttcli->connectToHost(); connect(mqttcli, &QMqttClient::connected, this, [this] { mqttcli->subscribe(QString("avu1"), 0); }); QThread::msleep(N*1000); //^^^^^^^^^^^^^^^^^^^^^ }
What is still not clear for me, why does it take N seconds to establish the connection? I mean – that's clear
connectToHost()
needs some time. But what I observe is it also needs to reach the end ofprep()
. What isconnectToHost()
thinking? Thanks for calling me, Sir, but I will not connect to host until the method that called me ends.I assume that I may have severe lack of knowledge in certain areas of programming, but that's the most surprising behavior I met so far. And I can't even know where to start digging. That's why I continue the thread, event though the code works.
Thanks for your time,
Pumice@Pumice said in qtmqtt connection issue:
QThread::msleep(N*1000);
Are you aware that this will block the event loop?
This is something you should NOT do when using asynchronous frameworks like Qt. It is not going to work. As long as the event loop is blocked your slot will not be called. That's why it looks like you need to reach the end of prep(). So remove that (I'm wondering why you put that sleep() there at all?). -
@jsulm, I was not aware of that. I feel I should go back to elementary.
Thank you very much! -
@jsulm, I was not aware of that. I feel I should go back to elementary.
Thank you very much!@Pumice have you checked the simple MQTT client example?
Putting the GUI things aside, you'll have the basic skeleton for a client with all the required steps (connection, disconnection, message received, etc.).
-
@Pumice have you checked the simple MQTT client example?
Putting the GUI things aside, you'll have the basic skeleton for a client with all the required steps (connection, disconnection, message received, etc.).
@Pablo-J.-Rogina of course I did. I was looking at all the examples for days before bothering you. Can't really explain why I didn't figure out what the problem was. It was clear for me that code in line n+1 is executed just after all the stuff related to the nth line is done. My all experience in async programming was several 5-liners in node.js. so far ;) thanks for all involved again.
-
@Pablo-J.-Rogina of course I did. I was looking at all the examples for days before bothering you. Can't really explain why I didn't figure out what the problem was. It was clear for me that code in line n+1 is executed just after all the stuff related to the nth line is done. My all experience in async programming was several 5-liners in node.js. so far ;) thanks for all involved again.
@Pumice said in qtmqtt connection issue:
I was looking at all the examples for days before bothering you.
This is what the forum is for, I mean, not to bother but to ask or share what may not be obvious at some point. Don't worry.
Once you know your issue is solved, please don't forget to mark your post as such.