mirror of
https://github.com/qt/qtmqtt.git
synced 2026-10-25 16:16:26 +00:00
Alternate text (alt text) improves the documentation experience for screen readers and for other accessibility tools. Task-number: QTBUG-135098 Pick-to: 6.11 Change-Id: Ia1793ff3e0bacd8cfdad2e8c04725a5093ed30aa Reviewed-by: David Boddie <david.boddie@qt.io>
100 lines
3.8 KiB
Plaintext
100 lines
3.8 KiB
Plaintext
// Copyright (C) 2018 The Qt Company Ltd.
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
|
|
|
|
/*!
|
|
\example simpleclient
|
|
\examplecategory {Connectivity}
|
|
\title Simple MQTT Client
|
|
\ingroup qtmqtt-examples
|
|
\brief Creating a minimalistic telemetry application.
|
|
|
|
\image simpleclient.png
|
|
{MQTT client with Host, Port, Protocol fields, WebSockets and Secure
|
|
options, Topic and Message inputs, and log showing state changes}
|
|
|
|
\e {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.
|
|
|
|
\section1 simpleclient Options
|
|
\list
|
|
\li Host - you may use test.mosquitto.org or broker.hivemq.com
|
|
\li Port - port to connect to, will depend on WebSockets selection and Secure selection
|
|
\li WebSockets - you may specify: selected (enabled) or not selected (disabled)
|
|
\li Secure - you may specify: selected (enabled) or not selected (disabled)
|
|
\li Protocol - you may specify
|
|
\list
|
|
\li mqtt 3.1
|
|
\li mqtt 3.1.1
|
|
\li mqtt 5.0
|
|
\endlist
|
|
\li Topic - A topic to publish or subscribe to
|
|
\li Message - A message to send on the topic if you are publishing
|
|
\endlist
|
|
|
|
\section1 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.
|
|
|
|
\section1
|
|
\list
|
|
\li Port 1883, Secure=disabled, WebSockets=disabled - mqtt over tcp
|
|
\li Port 8883, Secure=enabled, WebSockets=disabled - mqtt over ssl
|
|
\li Port 8080, Secure=disabled, WebSockets=enabled - mqtt over websockets
|
|
\li Port 8081, Secure=enabled, WebSockets=enabled - mqtt over secure websockets
|
|
\endlist
|
|
|
|
For more information about the brokers see:
|
|
\list
|
|
\li \l {https://test.mosquitto.org}
|
|
\li \l {https://broker.hivemq.com}
|
|
\li \l {https://www.hivemq.com/mqtt/public-mqtt-broker}
|
|
\endlist
|
|
|
|
\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.
|
|
|
|
\section1 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:
|
|
|
|
\quotefromfile simpleclient/mainwindow.cpp
|
|
\skipto m_client
|
|
\printuntil setPort
|
|
|
|
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:
|
|
|
|
\skipto messageReceived
|
|
\printuntil }
|
|
|
|
When users subscribe to topics in the client, we call
|
|
QMqttClient::subscribe() on the specified topic:
|
|
|
|
\skipto on_buttonSubscribe_clicked
|
|
\printuntil }
|
|
|
|
In this example, we subscribe to all topics. For more information about
|
|
how to receive messages on particular topics, see the
|
|
\l {MQTT Subscriptions} example.
|
|
|
|
For an example of how to use the QMqttClient class in a Qt Quick
|
|
application, see \l {Qt Quick Subscription}.
|
|
*/
|