Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. MQTT connection disconnected after connected successfully
Qt 6.12 LTS is out! See what's new in the release blog

MQTT connection disconnected after connected successfully

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.7k Views 1 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.
  • V Offline
    V Offline
    Vijay R. Gawade
    wrote on last edited by Vijay R. Gawade
    #1

    Hi All

    I am using Qt 5.9 with QMQTT-Emqx MQTT library for MQTT communication.

    Currently, we have locally installed a mosquitto broker and configured it to use an SSL certificate with the client certificate required on Port 8883.

    In our app, we connected to the broker with CA & Client certificate with the following code

        QSslConfiguration sslConfig;
        sslConfig.defaultConfiguration();
        sslConfig.setProtocol(QSsl::TlsV1_2);
        sslConfig.setPeerVerifyMode(QSslSocket::AutoVerifyPeer);
        sslConfig.setCaCertificates(caCertsList);
        sslConfig.setLocalCertificate(certs.first());
        sslConfig.setPrivateKey(QSslKey(clientKeyFile.readAll(), QSsl::Rsa));
    
        m_client = new QMQTT::Client("192.168.1.13", 8883, sslConfig, true);
        m_client->setClientId(MQTTConstant::clientId);
    
        connect(m_client, &QMQTT::Client::connected, this, &MqttController::onConnected);
        connect(m_client, &QMQTT::Client::disconnected, this, &MqttController::onDisconnected);
        connect(m_client, &QMQTT::Client::error, this, &MqttController::onError);
    
        m_client->setKeepAlive(10);
        m_client->setAutoReconnectInterval(1000);
        m_client->setCleanSession(true);
        m_client->setAutoReconnect(true);
        m_client->connectToHost();
    

    Also I checked SSL library details as follows:

    • SSL Library Build Version: "OpenSSL 1.0.2j 26 Sep 2016"
    • SSL Support: true
    • SSL Library Version: "OpenSSL 1.0.2e 3 Dec 2015"

    In onConnected() function I also able to publish messages successfully
    But after that following logs are appearing

    2022-06-30T16:24:00.639+05:30 ~ INF ~ controller.mqtt      ~ Disconnected from MQTT Broker !!!     isConnectedToHost false
    2022-06-30T16:24:00.643+05:30 ~ INF ~ controller.mqtt      ~ MqttController: Connected to MQTT Broker !!!
    2022-06-30T16:24:00.646+05:30 ~ INF ~ controller.mqtt      ~ Subscribing to sensor/rtdata topic:  true
    2022-06-30T16:24:00.652+05:30 ~ WRN ~ controller.mqtt      ~ Device is listening messages for Topic:  "sensor/rtdata"
    2022-06-30T16:24:00.654+05:30 ~ WRN ~ controller.mqtt      ~ New Message Arrived... Topic: "sensor/rtdata"    Payload: "This is test msg from windows-laptop"
    2022-06-30T16:24:00.657+05:30 ~ DBG ~ controller.mqtt      ~ MQTT SSL Error:  QMQTT::ClientError(SocketRemoteHostClosedError)    isConnectedToHost true
    2022-06-30T16:24:00.661+05:30 ~ DBG ~ controller.mqtt      ~ MQTT SSL Error:  QMQTT::ClientError(SocketRemoteHostClosedError)    isConnectedToHost true
    2022-06-30T16:24:00.664+05:30 ~ INF ~ controller.mqtt      ~ Disconnected from MQTT Broker !!!     isConnectedToHost false
    2022-06-30T16:24:00.667+05:30 ~ INF ~ controller.mqtt      ~ MqttController: Connected to MQTT Broker !!!
    2022-06-30T16:24:00.669+05:30 ~ WRN ~ controller.mqtt      ~ Device is not connected to MQTT Broker !!!
    2022-06-30T16:24:00.671+05:30 ~ INF ~ controller.mqtt      ~ Subscribing to sensor/rtdata topic:  false
    2022-06-30T16:24:00.744+05:30 ~ DBG ~ controller.mqtt      ~ MQTT SSL Error:  QMQTT::ClientError(SocketRemoteHostClosedError)    isConnectedToHost false
    2022-06-30T16:24:00.747+05:30 ~ DBG ~ controller.mqtt      ~ MQTT SSL Error:  QMQTT::ClientError(SocketRemoteHostClosedError)    isConnectedToHost false
    2022-06-30T16:24:00.750+05:30 ~ INF ~ controller.mqtt      ~ Disconnected from MQTT Broker !!!     isConnectedToHost false
    2022-06-30T16:24:00.752+05:30 ~ INF ~ controller.mqtt      ~ MqttController: Connected to MQTT Broker !!!
    2022-06-30T16:24:00.754+05:30 ~ WRN ~ controller.mqtt      ~ Device is not connected to MQTT Broker !!!
    2022-06-30T16:24:00.756+05:30 ~ INF ~ controller.mqtt      ~ Subscribing to sensor/rtdata topic:  false
    2022-06-30T16:24:00.788+05:30 ~ DBG ~ controller.mqtt      ~ MQTT SSL Error:  QMQTT::ClientError(SocketRemoteHostClosedError)    isConnectedToHost false
    2022-06-30T16:24:00.791+05:30 ~ DBG ~ controller.mqtt      ~ MQTT SSL Error:  QMQTT::ClientError(SocketRemoteHostClosedError)    isConnectedToHost false
    2022-06-30T16:24:00.794+05:30 ~ INF ~ controller.mqtt      ~ Disconnected from MQTT Broker !!!     isConnectedToHost false
    2022-06-30T16:24:00.798+05:30 ~ INF ~ controller.mqtt      ~ MqttController: Connected to MQTT Broker !!!
    2022-06-30T16:24:00.800+05:30 ~ WRN ~ controller.mqtt      ~ Device is not connected to MQTT Broker !!!
    2022-06-30T16:24:00.802+05:30 ~ INF ~ controller.mqtt      ~ Subscribing to sensor/rtdata topic:  false
    2022-06-30T16:24:00.907+05:30 ~ DBG ~ controller.mqtt      ~ MQTT SSL Error:  QMQTT::ClientError(SocketRemoteHostClosedError)    isConnectedToHost false
    2022-06-30T16:24:00.910+05:30 ~ DBG ~ controller.mqtt      ~ MQTT SSL Error:  QMQTT::ClientError(SocketRemoteHostClosedError)    isConnectedToHost false
    2022-06-30T16:24:00.913+05:30 ~ INF ~ controller.mqtt      ~ Disconnected from MQTT Broker !!!     isConnectedToHost false
    

    mosquitto configs:

    # secure port with client certificate required
    listener 8883
    allow_anonymous false
    cafile C:\Program Files\mosquitto\certs\ca.crt
    certfile C:\Program Files\mosquitto\certs\broker.crt
    keyfile C:\Program Files\mosquitto\certs\broker.key
    require_certificate true
    use_identity_as_username true
    tls_version tlsv1.2
    

    And above works as expected with mosquitto_sub/mosquitto_pub and another 3rd party MQTT X client tool without any issue

    $ mosquitto_pub -h 192.168.1.13 -p 8883 --cafile ca.crt --cert client.crt --key client.key -t test -m "This is message sent over secure port 8883 with client cert with CN as username"
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You should check that with the library author.

      Did you consider using the official QtMqtt module ?

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

      V 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        You should check that with the library author.

        Did you consider using the official QtMqtt module ?

        V Offline
        V Offline
        Vijay R. Gawade
        wrote on last edited by
        #3

        @SGaist Is the official QtMqtt module available for Qt 5.9? when I tried to import it for Qt 5.9, I was getting an unknown module error as follows:
        pro file:

        QT += mqtt
        QT += qmqtt
        

        While building getting following error:

        :-1: error: Unknown module(s) in QT: mqtt
        :-1: error: Project ERROR: Unknown module(s) in QT: qmqtt
        

        According to the qtmqtt repo, the minimum supported version is 5.10. That's why I am using the Emqx's qmqtt library.

        in which one open issue is there which look related to this:
        https://github.com/emqx/qmqtt/issues/187

        In which they are mentioning some bug related to QTcpSocket, When the remote peer uses shutdown any operation on its end, the local QTcpSocket closes the connection (RemoteHostClosedError).
        https://bugreports.qt.io/browse/QTBUG-39518

        @SGaist Can you please suggest to me which library or module I can use for MQTT with Qt 5.9?

        1 Reply Last reply
        0
        • C Offline
          C Offline
          ChrisW67
          wrote on last edited by
          #4

          @Vijay-R-Gawade Did you actually try it, or just assume that because there was no branch 5.9 that it was not possible?

          Others have had success: https://forum.qt.io/topic/101940/what-s-the-minimum-version-for-qtmqtt

          Is there a reason for developing a new project on an old version of Qt 5?

          V 1 Reply Last reply
          1
          • C ChrisW67

            @Vijay-R-Gawade Did you actually try it, or just assume that because there was no branch 5.9 that it was not possible?

            Others have had success: https://forum.qt.io/topic/101940/what-s-the-minimum-version-for-qtmqtt

            Is there a reason for developing a new project on an old version of Qt 5?

            V Offline
            V Offline
            Vijay R. Gawade
            wrote on last edited by Vijay R. Gawade
            #5

            @ChrisW67 yes, I have not tried to build it for Qt 5.8
            I will try to build & use this official module.

            And regarding the older qt version, yes we have device dependency, BSP not yet supported[in development]

            1 Reply Last reply
            0

            • Login

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