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. QT MQTT using port 8883 and TLS certificate
QtWS25 Last Chance

QT MQTT using port 8883 and TLS certificate

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 2 Posters 1.0k Views
  • 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.
  • L Offline
    L Offline
    lukutis222
    wrote on 26 Apr 2023, 19:14 last edited by
    #1

    Hello. I have been playing around with MQTT lately.

    I used test.mosquitto.org server with a default TCP port 1883. Everything seemed to work fine.

    I decided to try out secure port 8883 that requires TLS certificate. I looked up a relevant forum thread regarding this:
    https://forum.qt.io/topic/118694/adding-tls-functionality-to-qt-mqtt-simpleclient-example

    And i modified on connect button click as following:

    void MainWindow::on_buttonConnect_clicked()
    {
    
        QByteArray ba_crt;
    
        QFile ca_crtf("C:/Program Files/mosquitto/certs_v4/mosquitto.org.crt");
        if (!ca_crtf.open(QIODevice::ReadOnly)) {
            qDebug() << "ERROR: Opening file: ca.crt";
        } else {
            ba_crt = ca_crtf.readAll();
            ca_crtf.close();
            qDebug() << "Read CA certificate";
        }
    
        QSslCertificate ca_crt(ba_crt, QSsl::Pem);
        QSslConfiguration config;
        config.defaultConfiguration();
        config.setProtocol(QSsl::TlsV1_2);			// needs to be handled explicitly with
        config.addCaCertificate(ca_crt);			// client.connectToHostEncrypted(config);
        config.setPeerVerifyMode(QSslSocket::VerifyNone);	// Dont check hostname from certificate
    
    
        if (m_client->state() == QMqttClient::Disconnected) {
            ui->lineEditHost->setEnabled(false);
            ui->spinBoxPort->setEnabled(false);
            ui->buttonConnect->setText(tr("Disconnect"));
            //m_client->connectToHost();
            m_client->connectToHostEncrypted(config);
        } else {
            ui->lineEditHost->setEnabled(true);
            ui->spinBoxPort->setEnabled(true);
            ui->buttonConnect->setText(tr("Connect"));
            m_client->disconnectFromHost();
        }
    }
    

    I downloaded mosquitto TLS certificate from official website:
    https://test.mosquitto.org/

    I have managed to establish a connection without any issues but after exactly 1 minute, I got disconnected :
    9fd925e2-f778-4c0d-8538-4b9f7623c8a1-image.png

    Normally, when I use unsecure port (1883), the ping is issues every 1 minute , I believe it might have something to do with the fact that I am being disconnected when using port 8883. Notice ping response when using 1883 port:

    9057ffb7-5466-4cf5-8c88-1f25e0ae9d11-image.png

    If you have any ideas what could be wrong here, please let me know! Thanks in advance

    S 1 Reply Last reply 26 Apr 2023, 19:31
    0
    • L lukutis222
      26 Apr 2023, 19:14

      Hello. I have been playing around with MQTT lately.

      I used test.mosquitto.org server with a default TCP port 1883. Everything seemed to work fine.

      I decided to try out secure port 8883 that requires TLS certificate. I looked up a relevant forum thread regarding this:
      https://forum.qt.io/topic/118694/adding-tls-functionality-to-qt-mqtt-simpleclient-example

      And i modified on connect button click as following:

      void MainWindow::on_buttonConnect_clicked()
      {
      
          QByteArray ba_crt;
      
          QFile ca_crtf("C:/Program Files/mosquitto/certs_v4/mosquitto.org.crt");
          if (!ca_crtf.open(QIODevice::ReadOnly)) {
              qDebug() << "ERROR: Opening file: ca.crt";
          } else {
              ba_crt = ca_crtf.readAll();
              ca_crtf.close();
              qDebug() << "Read CA certificate";
          }
      
          QSslCertificate ca_crt(ba_crt, QSsl::Pem);
          QSslConfiguration config;
          config.defaultConfiguration();
          config.setProtocol(QSsl::TlsV1_2);			// needs to be handled explicitly with
          config.addCaCertificate(ca_crt);			// client.connectToHostEncrypted(config);
          config.setPeerVerifyMode(QSslSocket::VerifyNone);	// Dont check hostname from certificate
      
      
          if (m_client->state() == QMqttClient::Disconnected) {
              ui->lineEditHost->setEnabled(false);
              ui->spinBoxPort->setEnabled(false);
              ui->buttonConnect->setText(tr("Disconnect"));
              //m_client->connectToHost();
              m_client->connectToHostEncrypted(config);
          } else {
              ui->lineEditHost->setEnabled(true);
              ui->spinBoxPort->setEnabled(true);
              ui->buttonConnect->setText(tr("Connect"));
              m_client->disconnectFromHost();
          }
      }
      

      I downloaded mosquitto TLS certificate from official website:
      https://test.mosquitto.org/

      I have managed to establish a connection without any issues but after exactly 1 minute, I got disconnected :
      9fd925e2-f778-4c0d-8538-4b9f7623c8a1-image.png

      Normally, when I use unsecure port (1883), the ping is issues every 1 minute , I believe it might have something to do with the fact that I am being disconnected when using port 8883. Notice ping response when using 1883 port:

      9057ffb7-5466-4cf5-8c88-1f25e0ae9d11-image.png

      If you have any ideas what could be wrong here, please let me know! Thanks in advance

      S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 26 Apr 2023, 19:31 last edited by
      #2

      Hi,

      Did you connect the error related signal ?

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

      L 1 Reply Last reply 27 Apr 2023, 04:45
      0
      • S SGaist
        26 Apr 2023, 19:31

        Hi,

        Did you connect the error related signal ?

        L Offline
        L Offline
        lukutis222
        wrote on 27 Apr 2023, 04:45 last edited by lukutis222
        #3

        @SGaist

        Good suggestion. I have added the following signal:

        connect(m_client, &QMqttClient::errorChanged, this, &MainWindow::errorHandler);
        

        And I simply print the error code:

        void MainWindow::errorHandler(){
            qDebug("Error detected = %u",m_client->error());
        }
        

        After 1 minute, in the console I see:

        Error detected = 256
        

        According to QT documentation:
        7ba43fe3-8adf-46b3-a737-1e45865f49c4-image.png

        I am convinced that it is related to the ping. According to QT documentation:
        04072511-77e8-4931-9ea2-af18ef315703-image.png

        I have tried the following:

        m_client->setAutoKeepAlive(false);
        

        When I set AutoKeepAlive to false, I must manually request a ping.

        Just for testing purpose, I have added a requestPing() everytime I try to publish something:

        void MainWindow::on_buttonPublish_clicked()
        {
            if (m_client->publish(ui->lineEditTopic->text(), ui->lineEditMessage->text().toUtf8()) == -1)
                QMessageBox::critical(this, QLatin1String("Error"), QLatin1String("Could not publish message"));
            m_client->requestPing();
        }
        

        4016a1b2-9ff5-4144-afb7-9150b318a580-image.png

        As you can see from image above, as long as I ping within KeepAlive interval, the connection will stay active.

        However I am still not sure why it works that way. If AutoKeepAlive is set to true, the ping request should be issues automatically.

        L 1 Reply Last reply 27 Apr 2023, 19:18
        0
        • L lukutis222
          27 Apr 2023, 04:45

          @SGaist

          Good suggestion. I have added the following signal:

          connect(m_client, &QMqttClient::errorChanged, this, &MainWindow::errorHandler);
          

          And I simply print the error code:

          void MainWindow::errorHandler(){
              qDebug("Error detected = %u",m_client->error());
          }
          

          After 1 minute, in the console I see:

          Error detected = 256
          

          According to QT documentation:
          7ba43fe3-8adf-46b3-a737-1e45865f49c4-image.png

          I am convinced that it is related to the ping. According to QT documentation:
          04072511-77e8-4931-9ea2-af18ef315703-image.png

          I have tried the following:

          m_client->setAutoKeepAlive(false);
          

          When I set AutoKeepAlive to false, I must manually request a ping.

          Just for testing purpose, I have added a requestPing() everytime I try to publish something:

          void MainWindow::on_buttonPublish_clicked()
          {
              if (m_client->publish(ui->lineEditTopic->text(), ui->lineEditMessage->text().toUtf8()) == -1)
                  QMessageBox::critical(this, QLatin1String("Error"), QLatin1String("Could not publish message"));
              m_client->requestPing();
          }
          

          4016a1b2-9ff5-4144-afb7-9150b318a580-image.png

          As you can see from image above, as long as I ping within KeepAlive interval, the connection will stay active.

          However I am still not sure why it works that way. If AutoKeepAlive is set to true, the ping request should be issues automatically.

          L Offline
          L Offline
          lukutis222
          wrote on 27 Apr 2023, 19:18 last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • L Offline
            L Offline
            lukutis222
            wrote on 27 Apr 2023, 19:20 last edited by lukutis222
            #5

            It is still unclear why MQTT over secure port 8883 is not working correctly with default settings ( autoKeepAlive set to TRUE to ping every 60 seconds).

            Could it be a QT issue or thats not related at all?

            S 1 Reply Last reply 27 Apr 2023, 19:26
            0
            • L lukutis222
              27 Apr 2023, 19:20

              It is still unclear why MQTT over secure port 8883 is not working correctly with default settings ( autoKeepAlive set to TRUE to ping every 60 seconds).

              Could it be a QT issue or thats not related at all?

              S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 27 Apr 2023, 19:26 last edited by
              #6

              Did you try to change the keep alive interval ?

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

              L 1 Reply Last reply 29 Apr 2023, 08:38
              0
              • S SGaist
                27 Apr 2023, 19:26

                Did you try to change the keep alive interval ?

                L Offline
                L Offline
                lukutis222
                wrote on 29 Apr 2023, 08:38 last edited by lukutis222
                #7

                @SGaist

                Good suggestion.

                I have tried to set it to less than 60 and that seems to work fine.

                In the example below, I have set keepalive to 10 seconds and as you can see I am not getting disconnected anymore.
                45831d5f-6d54-443d-9f3f-7fdd6c3755da-image.png

                I have then set it to 65 seconds and started the program again. Guess what - I have been disconnected exactly after 60 seconds.
                60e35722-271a-47a0-9788-0520786d590b-image.png

                So according to this test, keepalive must be set to less than 60 seconds when using port 8883 in order to prevent the client from disconnecting. But when using normal port (1883) it does not seem to matter at all. I have tried to read official MQTT documentation regarding keepalive but I cannot find anything that suggests keepalive should be less than 60. Is there any way to debug this further to determine if this is QT bug ?

                S 1 Reply Last reply 29 Apr 2023, 16:13
                0
                • L lukutis222
                  29 Apr 2023, 08:38

                  @SGaist

                  Good suggestion.

                  I have tried to set it to less than 60 and that seems to work fine.

                  In the example below, I have set keepalive to 10 seconds and as you can see I am not getting disconnected anymore.
                  45831d5f-6d54-443d-9f3f-7fdd6c3755da-image.png

                  I have then set it to 65 seconds and started the program again. Guess what - I have been disconnected exactly after 60 seconds.
                  60e35722-271a-47a0-9788-0520786d590b-image.png

                  So according to this test, keepalive must be set to less than 60 seconds when using port 8883 in order to prevent the client from disconnecting. But when using normal port (1883) it does not seem to matter at all. I have tried to read official MQTT documentation regarding keepalive but I cannot find anything that suggests keepalive should be less than 60. Is there any way to debug this further to determine if this is QT bug ?

                  S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 29 Apr 2023, 16:13 last edited by
                  #8

                  You can use a different client library to connect to this server.

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

                  L 1 Reply Last reply 29 Apr 2023, 18:32
                  0
                  • S SGaist
                    29 Apr 2023, 16:13

                    You can use a different client library to connect to this server.

                    L Offline
                    L Offline
                    lukutis222
                    wrote on 29 Apr 2023, 18:32 last edited by lukutis222
                    #9

                    @SGaist
                    I am not fully understanding what you mean different client library? Can you please clarify?
                    From what I know, qtmqtt is the only official library for mqtt that is supported by QT so if anything, it should be more reliable than some custom mqtt libraries out there.

                    Do you think that this issue is from the client side (QT) ?

                    S 1 Reply Last reply 29 Apr 2023, 18:37
                    0
                    • L lukutis222
                      29 Apr 2023, 18:32

                      @SGaist
                      I am not fully understanding what you mean different client library? Can you please clarify?
                      From what I know, qtmqtt is the only official library for mqtt that is supported by QT so if anything, it should be more reliable than some custom mqtt libraries out there.

                      Do you think that this issue is from the client side (QT) ?

                      S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 29 Apr 2023, 18:37 last edited by
                      #10

                      It could be, the idea is to try another library such as paho or maybe mosquittopp to determine whether the issue is server or client side.

                      Note that I have used neither of these libraries.

                      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
                      0

                      6/10

                      27 Apr 2023, 19:26

                      • Login

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