Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. qtmqtt connection issue
Forum Updated to NodeBB v4.3 + New Features

qtmqtt connection issue

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 1.8k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pumice
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • P Offline
        P Offline
        Pumice
        wrote on last edited by
        #3

        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

        jsulmJ 1 Reply Last reply
        0
        • P 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

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by jsulm
          #4

          @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.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • P Offline
            P Offline
            Pumice
            wrote on last edited by
            #5

            @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 of prep(). What is connectToHost() 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

            jsulmJ 1 Reply Last reply
            0
            • P 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 of prep(). What is connectToHost() 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

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @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?).

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              4
              • P Offline
                P Offline
                Pumice
                wrote on last edited by
                #7

                @jsulm, I was not aware of that. I feel I should go back to elementary.
                Thank you very much!

                jsulmJ Pablo J. RoginaP 2 Replies Last reply
                0
                • P Pumice

                  @jsulm, I was not aware of that. I feel I should go back to elementary.
                  Thank you very much!

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Pumice Asynchronous programming is somewhat difficult at the beginning :-)

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • P Pumice

                    @jsulm, I was not aware of that. I feel I should go back to elementary.
                    Thank you very much!

                    Pablo J. RoginaP Offline
                    Pablo J. RoginaP Offline
                    Pablo J. Rogina
                    wrote on last edited by
                    #9

                    @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.).

                    Upvote the answer(s) that helped you solve the issue
                    Use "Topic Tools" button to mark your post as Solved
                    Add screenshots via postimage.org
                    Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                    P 1 Reply Last reply
                    2
                    • Pablo J. RoginaP Pablo J. Rogina

                      @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.).

                      P Offline
                      P Offline
                      Pumice
                      wrote on last edited by
                      #10

                      @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. RoginaP 1 Reply Last reply
                      1
                      • P Pumice

                        @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. RoginaP Offline
                        Pablo J. RoginaP Offline
                        Pablo J. Rogina
                        wrote on last edited by
                        #11

                        @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.

                        Upvote the answer(s) that helped you solve the issue
                        Use "Topic Tools" button to mark your post as Solved
                        Add screenshots via postimage.org
                        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                        1 Reply Last reply
                        1

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved