Simple MQTT Client
Creating a minimalistic telemetry application.
Simple MQTT Client demonstrates how to create a minimalistic client application.
To use the application, you first specify a Host, a Port, Secure on/off, WebSockets on/off, and Protocol. Then click on connect. Afterward you can subscribe to a topic and send a message, which you will also receive. You may also run the application twice. Publish in one and Subscribe in the other, and the messages shall be transmitted.
simpleclient Options
- Host - you may use test.mosquitto.org or broker.hivemq.com
- Port - port to connect to, will depend on WebSockets selection and Secure selection
- WebSockets - you may specify: selected (enabled) or not selected (disabled)
- Secure - you may specify: selected (enabled) or not selected (disabled)
- Protocol - you may specify
- mqtt 3.1
- mqtt 3.1.1
- mqtt 5.0
- Topic - A topic to publish or subscribe to
- Message - A message to send on the topic if you are publishing
Connection Settings
These are the usual combinations, and will work with test.mosquitto.org. For broker.hivemq.com check the information in the links. Note that if Secure is enabled it might be necessary to install certificates on the computer. It might also be necessary to add the certificate to the mqtt client in code.
- Port 1883, Secure=disabled, WebSockets=disabled - mqtt over tcp
- Port 8883, Secure=enabled, WebSockets=disabled - mqtt over ssl
- Port 8080, Secure=disabled, WebSockets=enabled - mqtt over websockets
- Port 8081, Secure=enabled, WebSockets=enabled - mqtt over secure websockets
For more information about the brokers see:
Note: Port numbers 1883 and 8080 are not encrypted, and therefore they are suitable only for development and testing purposes. In production, always use encrypted connections.
Note: WebAssembly only supports WebSockets enabled.
Creating a Client
First, we use the QMqttClient class to create an MQTT client. The class provides properties for setting a unique client ID as well as the broker host name and port to connect to:
m_client = new QMqttClient(this); m_client->setHostname(ui->lineEditHost->text()); m_client->setPort(static_cast<quint16>(ui->spinBoxPort->value()));
We do not set the client ID, and therefore it will be automatically generated for us.
Next, we connect to QMqttClient::messageReceived() to receive all messages sent to the broker:
connect(m_client, &QMqttClient::messageReceived, this, [this](const QByteArray &message, const QMqttTopicName &topic) { const QString content = QDateTime::currentDateTime().toString() + " Received Topic: "_L1 + topic.name() + " Message: "_L1 + message + u'\n'; ui->editLog->insertPlainText(content); });
When users subscribe to topics in the client, we call QMqttClient::subscribe() on the specified topic:
void MainWindow::on_buttonSubscribe_clicked() { auto subscription = m_client->subscribe(ui->lineEditTopic->text()); if (!subscription) { QMessageBox::critical(this, u"Error"_s, u"Could not subscribe. Is there a valid connection?"_s); return; }
In this example, we subscribe to all topics. For more information about how to receive messages on particular topics, see the MQTT Subscriptions example.
For an example of how to use the QMqttClient class in a Qt Quick application, see Qt Quick Subscription.
Files:
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.