QMqttClient#

The QMqttClient class represents the central access communicating with an MQTT broker. More

Inheritance diagram of PySide6.QtMqtt.QMqttClient

Synopsis#

Properties#

  • autoKeepAlive - This property holds whether the client will automatically manage keep alive messages to the server

  • cleanSession - This property holds the state after connecting to a broker

  • clientId - This property holds the client’s identifier value

  • error - Specifies the current error of the client

  • hostname - This property holds the hostname of the MQTT broker to connect to

  • keepAlive - This property holds the interval at which regular ping messages are sent to the broker

  • password - This property holds the password for connecting to a broker

  • port - This property holds the port to connect to the MQTT broker

  • protocolVersion - This property holds the MQTT standard version to use for connections

  • state - This property holds the current state of the client

  • username - This property holds the user name for connecting to a broker

  • willMessage - This property holds the payload of a Will Message

  • willQoS - This property holds the level of QoS for sending and storing the Will Message

  • willRetain - This property holds whether the Will Message should be retained on the broker for future subscribers to receive

  • willTopic - This property holds the Will Topic

Functions#

Slots#

Signals#

Note

This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE

Detailed Description#

An MQTT client is a program or device that uses MQTT to create a network connection to an MQTT server, also called a broker. The connection request must contain a unique client identifier. Optionally, it can contain a Will Topic, Will Message, user name, and password.

Once a connection is created, a client can send messages that other clients might be interested in receiving, subscribe to request notifications on topics, unsubscribe to remove a request for notifications, and disconnect from the broker.

class PySide6.QtMqtt.QMqttClient([parent=None])#
Parameters:

parentPySide6.QtCore.QObject

Creates a new MQTT client instance with the specified parent.

Note

Properties can be used directly when from __feature__ import true_property is used or via accessor functions otherwise.

property PᅟySide6.QtMqtt.QMqttClient.autoKeepAlive: bool#

This property holds This property holds whether the client will automatically manage keep alive messages to the server..

If this property is true, then the client will automatically send a ping message to the server at the keepAlive interval.

Otherwise, a user will have to manually invoke requestPing within the specified interval of the connection. If no ping has been sent within the interval, the server will disconnect.

The default of this property is true.

Access functions:
property PᅟySide6.QtMqtt.QMqttClient.cleanSession: bool#

This property holds This property holds the state after connecting to a broker..

Access functions:
property PᅟySide6.QtMqtt.QMqttClient.clientId: str#

This property holds This property holds the client’s identifier value..

Each client needs to have a unique ID to be able to connect to an MQTT broker. If no client ID is specified by the user, one will be generated automatically when a connection is established.

Access functions:
property PᅟySide6.QtMqtt.QMqttClient.error: ClientError#

This property Specifies the current error of the client..

Access functions:
property PᅟySide6.QtMqtt.QMqttClient.hostname: str#

This property holds This property holds the hostname of the MQTT broker to connect to..

If no transport is specified via setTransport() , the client will instantiate a socket connection to the specified hostname itself.

Access functions:
property PᅟySide6.QtMqtt.QMqttClient.keepAlive: int#

This property holds This property holds the interval at which regular ping messages are sent to the broker..

Once a connection to a broker is established, the client needs to send frequent updates to propagate it can still be reached. The interval between those updates is specified by this property.

The interval is specified in seconds.

If the broker does not respond within a grace period the connection will be closed.

Access functions:
property PᅟySide6.QtMqtt.QMqttClient.password: str#

This property holds This property holds the password for connecting to a broker..

Access functions:
property PᅟySide6.QtMqtt.QMqttClient.port: int#

This property holds This property holds the port to connect to the MQTT broker..

If no transport is specified via setTransport() , the client will instantiate a socket connection to a host with this port number.

Access functions:
property PᅟySide6.QtMqtt.QMqttClient.protocolVersion: ProtocolVersion#

This property holds This property holds the MQTT standard version to use for connections..

Specifies the version of the standard the client uses for connecting to a broker. Valid values are:

  • 3: MQTT standard version 3.1.

  • 4: MQTT standard version 3.1.1, often referred to MQTT 4.

Access functions:
property PᅟySide6.QtMqtt.QMqttClient.state: ClientState#

This property holds This property holds the current state of the client..

Access functions:
property PᅟySide6.QtMqtt.QMqttClient.username: str#

This property holds This property holds the user name for connecting to a broker..

Access functions:
property PᅟySide6.QtMqtt.QMqttClient.willMessage: PySide6.QtCore.QByteArray#

This property holds This property holds the payload of a Will Message..

Access functions:
property PᅟySide6.QtMqtt.QMqttClient.willQoS: int#

This property holds This property holds the level of QoS for sending and storing the Will Message..

Access functions:
property PᅟySide6.QtMqtt.QMqttClient.willRetain: bool#

This property holds This property holds whether the Will Message should be retained on the broker for future subscribers to receive..

Access functions:
property PᅟySide6.QtMqtt.QMqttClient.willTopic: str#

This property holds This property holds the Will Topic..

Access functions:
PySide6.QtMqtt.QMqttClient.TransportType#

This enum type specifies the connection method to be used to instantiate a connection to a broker.

Constant

Description

QMqttClient.IODevice

The transport uses a class based on a QIODevice.

QMqttClient.AbstractSocket

The transport uses a class based on a QAbstractSocket.

QMqttClient.SecureSocket

The transport uses a class based on a QSslSocket.

PySide6.QtMqtt.QMqttClient.ClientState#

This enum type specifies the states a client can enter.

Constant

Description

QMqttClient.Disconnected

The client is disconnected from the broker.

QMqttClient.Connecting

A connection request has been made, but the broker has not approved the connection yet.

QMqttClient.Connected

The client is connected to the broker.

PySide6.QtMqtt.QMqttClient.ClientError#

This enum type specifies the error state of a client.

Constant

Description

QMqttClient.NoError

No error occurred.

QMqttClient.InvalidProtocolVersion

The broker does not accept a connection using the specified protocol version.

QMqttClient.IdRejected

The client ID is malformed. This might be related to its length.

QMqttClient.ServerUnavailable

The network connection has been established, but the service is unavailable on the broker side.

QMqttClient.BadUsernameOrPassword

The data in the username or password is malformed.

QMqttClient.NotAuthorized

The client is not authorized to connect.

QMqttClient.TransportInvalid

The underlying transport caused an error. For example, the connection might have been interrupted unexpectedly.

QMqttClient.ProtocolViolation

The client encountered a protocol violation, and therefore closed the connection.

QMqttClient.UnknownError

An unknown error occurred.

QMqttClient.Mqtt5SpecificError

The error is related to MQTT protocol level 5. A reason code might provide more details.

PySide6.QtMqtt.QMqttClient.ProtocolVersion#

This enum specifies the protocol version of the MQTT standard to use during communication with a broker.

Constant

Description

QMqttClient.MQTT_3_1

MQTT Standard 3.1

QMqttClient.MQTT_3_1_1

MQTT Standard 3.1.1, publicly referred to as version 4

QMqttClient.MQTT_5_0

MQTT Standard 5.0

PySide6.QtMqtt.QMqttClient.authenticate(prop)#
Parameters:

propPySide6.QtMqtt.QMqttAuthenticationProperties

Sends an authentication request to the broker. prop specifies the required information to fulfill the authentication request.

This function should only be called after a authenticationRequested signal has been emitted.

Note

Extended authentication is part of the MQTT 5.0 standard and can only be used when the client specifies MQTT_5_0 as ProtocolVersion .

PySide6.QtMqtt.QMqttClient.authenticationFinished(p)#
Parameters:

pPySide6.QtMqtt.QMqttAuthenticationProperties

This signal is emitted after extended authentication has finished. p specifies available details on the authentication process.

After successful authentication connected is emitted.

Note

Extended authentication is part of the MQTT 5.0 standard and can only be used when the client specifies MQTT_5_0 as ProtocolVersion .

PySide6.QtMqtt.QMqttClient.authenticationRequested(p)#
Parameters:

pPySide6.QtMqtt.QMqttAuthenticationProperties

This signal is emitted after a client invoked connectToHost or connectToHostEncrypted and before the connection is established. In extended authentication, a broker might request additional details which need to be provided by invoking authenticate . p specifies properties provided by the broker.

Note

Extended authentication is part of the MQTT 5.0 standard and can only be used when the client specifies MQTT_5_0 as ProtocolVersion .

PySide6.QtMqtt.QMqttClient.autoKeepAlive()#
Return type:

bool

Getter of property autoKeepAlive .

PySide6.QtMqtt.QMqttClient.autoKeepAliveChanged(autoKeepAlive)#
Parameters:

autoKeepAlive – bool

Notification signal of property autoKeepAlive .

PySide6.QtMqtt.QMqttClient.brokerSessionRestored()#

This signal is emitted after a client has successfully connected to a broker with the cleanSession property set to false, and the broker has restored the session.

Sessions can be restored if a client has connected previously using the same clientId .

PySide6.QtMqtt.QMqttClient.cleanSession()#
Return type:

bool

Getter of property cleanSession .

PySide6.QtMqtt.QMqttClient.cleanSessionChanged(cleanSession)#
Parameters:

cleanSession – bool

Notification signal of property cleanSession .

PySide6.QtMqtt.QMqttClient.clientId()#
Return type:

str

See also

setClientId()

Getter of property clientId .

PySide6.QtMqtt.QMqttClient.clientIdChanged(clientId)#
Parameters:

clientId – str

Notification signal of property clientId .

PySide6.QtMqtt.QMqttClient.connectToHost()#

Initiates a connection to the MQTT broker.

PySide6.QtMqtt.QMqttClient.connectToHostEncrypted(conf)#
Parameters:

confPySide6.QtNetwork.QSslConfiguration

Initiates an encrypted connection to the MQTT broker.

conf specifies the SSL configuration to be used for the connection

PySide6.QtMqtt.QMqttClient.connected()#

This signal is emitted when a connection has been established.

PySide6.QtMqtt.QMqttClient.connectionProperties()#
Return type:

PySide6.QtMqtt.QMqttConnectionProperties

Returns the connection properties the client requests to the broker.

Note

QMqttConnectionProperties can only be used when the client specifies MQTT_5_0 as ProtocolVersion .

PySide6.QtMqtt.QMqttClient.disconnectFromHost()#

Disconnects from the MQTT broker.

PySide6.QtMqtt.QMqttClient.disconnected()#

This signal is emitted when a connection has been closed. A connection may be closed when disconnectFromHost() is called or when the broker disconnects.

PySide6.QtMqtt.QMqttClient.error()#
Return type:

ClientError

See also

setError()

Getter of property error .

PySide6.QtMqtt.QMqttClient.errorChanged(error)#
Parameters:

errorClientError

Notification signal of property error .

PySide6.QtMqtt.QMqttClient.hostname()#
Return type:

str

See also

setHostname()

Getter of property hostname .

PySide6.QtMqtt.QMqttClient.hostnameChanged(hostname)#
Parameters:

hostname – str

Notification signal of property hostname .

PySide6.QtMqtt.QMqttClient.keepAlive()#
Return type:

int

See also

setKeepAlive()

Getter of property keepAlive .

PySide6.QtMqtt.QMqttClient.keepAliveChanged(keepAlive)#
Parameters:

keepAlive – int

Notification signal of property keepAlive .

PySide6.QtMqtt.QMqttClient.lastWillProperties()#
Return type:

PySide6.QtMqtt.QMqttLastWillProperties

Returns the last will properties.

Note

QMqttLastWillProperties can only be used when the client specifies MQTT_5_0 as ProtocolVersion .

PySide6.QtMqtt.QMqttClient.messageReceived(message[, topic=QMqttTopicName()])#
Parameters:

This signal is emitted when a new message has been received. The category of the message is specified by topic with the content being message.

PySide6.QtMqtt.QMqttClient.messageSent(id)#
Parameters:

id – int

Indicates that a message that was sent via the publish() function has been received by the broker. The id is the same as returned by publish() to help tracking the status of the message.

PySide6.QtMqtt.QMqttClient.messageStatusChanged(id, s, properties)#
Parameters:

This signal is emitted when the status for the message identified by id changes. s specifies the new status of the message, and properties specify additional properties provided by the server.

PySide6.QtMqtt.QMqttClient.password()#
Return type:

str

See also

setPassword()

Getter of property password .

PySide6.QtMqtt.QMqttClient.passwordChanged(password)#
Parameters:

password – str

Notification signal of property password .

PySide6.QtMqtt.QMqttClient.pingResponseReceived()#

This signal is emitted after the broker responds to a requestPing() call or a keepAlive() ping message, and the connection is still valid.

PySide6.QtMqtt.QMqttClient.port()#
Return type:

int

See also

setPort()

Getter of property port .

PySide6.QtMqtt.QMqttClient.portChanged(port)#
Parameters:

port – int

Notification signal of property port .

PySide6.QtMqtt.QMqttClient.protocolVersion()#
Return type:

ProtocolVersion

Getter of property protocolVersion .

PySide6.QtMqtt.QMqttClient.protocolVersionChanged(protocolVersion)#
Parameters:

protocolVersionProtocolVersion

Notification signal of property protocolVersion .

PySide6.QtMqtt.QMqttClient.publish(topic[, message=QByteArray()[, qos=0[, retain=false]]])#
Parameters:
Return type:

int

Publishes a message to the broker with the specified topic. qos specifies the QoS level required for transferring the message.

If retain is set to true, the message will stay on the broker for other clients to connect and receive the message.

Returns an ID that is used internally to identify the message.

PySide6.QtMqtt.QMqttClient.publish(topic, properties[, message=QByteArray()[, qos=0[, retain=false]]])
Parameters:
Return type:

int

Publishes a message to the broker with the specified properties and topic. qos specifies the QoS level required for transferring the message.

If retain is set to true, the message will stay on the broker for other clients to connect and receive the message.

Returns an ID that is used internally to identify the message.

Note

properties will only be passed to the broker when the client specifies MQTT_5_0 as ProtocolVersion .

PySide6.QtMqtt.QMqttClient.requestPing()#
Return type:

bool

Sends a ping message to the broker and expects a reply.

If the connection is active and autoKeepAlive is true, then calling this function will fail as the client is responsible for managing this process.

Using requestPing() manually requires a call every time within the keepAlive interval as long as the connection is active.

To check whether the ping is successful, connect to the pingResponseReceived() signal.

Returns true if the ping request could be sent.

PySide6.QtMqtt.QMqttClient.serverConnectionProperties()#
Return type:

PySide6.QtMqtt.QMqttServerConnectionProperties

Returns the QMqttServerConnectionProperties the broker returned after a connection attempt.

This can be used to verify that client side connection properties set by setConnectionProperties have been accepted by the broker. Also, in case of a failed connection attempt, it can be used for connection diagnostics.

Note

QMqttServerConnectionProperties can only be used when the client specifies MQTT_5_0 as ProtocolVersion .

PySide6.QtMqtt.QMqttClient.setAutoKeepAlive(autoKeepAlive)#
Parameters:

autoKeepAlive – bool

See also

autoKeepAlive()

Setter of property autoKeepAlive .

PySide6.QtMqtt.QMqttClient.setCleanSession(cleanSession)#
Parameters:

cleanSession – bool

See also

cleanSession()

Setter of property cleanSession .

PySide6.QtMqtt.QMqttClient.setClientId(clientId)#
Parameters:

clientId – str

See also

clientId()

Setter of property clientId .

PySide6.QtMqtt.QMqttClient.setConnectionProperties(prop)#
Parameters:

propPySide6.QtMqtt.QMqttConnectionProperties

Sets the connection properties to prop. QMqttConnectionProperties can be used to ask the server to use a specific feature set. After a connection request the server response can be obtained by calling serverConnectionProperties .

Note

The connection properties can only be set if the MQTT client is in the Disconnected state.

Note

QMqttConnectionProperties can only be used when the client specifies MQTT_5_0 as ProtocolVersion .

PySide6.QtMqtt.QMqttClient.setError(error)#
Parameters:

errorClientError

See also

error()

Setter of property error .

PySide6.QtMqtt.QMqttClient.setHostname(hostname)#
Parameters:

hostname – str

See also

hostname()

Setter of property hostname .

PySide6.QtMqtt.QMqttClient.setKeepAlive(keepAlive)#
Parameters:

keepAlive – int

See also

keepAlive()

Setter of property keepAlive .

PySide6.QtMqtt.QMqttClient.setLastWillProperties(prop)#
Parameters:

propPySide6.QtMqtt.QMqttLastWillProperties

Sets the last will properties to prop. QMqttLastWillProperties allows to set additional features for the last will message stored at the broker.

Note

The connection properties can only be set if the MQTT client is in the Disconnected state.

Note

QMqttLastWillProperties can only be used when the client specifies MQTT_5_0 as ProtocolVersion .

PySide6.QtMqtt.QMqttClient.setPassword(password)#
Parameters:

password – str

See also

password()

Setter of property password .

PySide6.QtMqtt.QMqttClient.setPort(port)#
Parameters:

port – int

See also

port()

Setter of property port .

PySide6.QtMqtt.QMqttClient.setProtocolVersion(protocolVersion)#
Parameters:

protocolVersionProtocolVersion

Setter of property protocolVersion .

PySide6.QtMqtt.QMqttClient.setState(state)#
Parameters:

stateClientState

See also

state()

Setter of property state .

PySide6.QtMqtt.QMqttClient.setTransport(device, transport)#
Parameters:

Sets the transport to device. A transport can be either a socket type or derived from QIODevice and is specified by transport.

Note

The transport can only be exchanged if the MQTT client is in the Disconnected state.

Note

Setting a custom transport for a client does not pass over responsibility on connection management. The transport has to be opened for QIODevice based transports or connected for socket type transports before calling connectToHost() .

See also

transport()

PySide6.QtMqtt.QMqttClient.setUsername(username)#
Parameters:

username – str

See also

username()

Setter of property username .

PySide6.QtMqtt.QMqttClient.setWillMessage(willMessage)#
Parameters:

willMessagePySide6.QtCore.QByteArray

See also

willMessage()

Setter of property willMessage .

PySide6.QtMqtt.QMqttClient.setWillQoS(willQoS)#
Parameters:

willQoS – int

See also

willQoS()

Setter of property willQoS .

PySide6.QtMqtt.QMqttClient.setWillRetain(willRetain)#
Parameters:

willRetain – bool

See also

willRetain()

Setter of property willRetain .

PySide6.QtMqtt.QMqttClient.setWillTopic(willTopic)#
Parameters:

willTopic – str

See also

willTopic()

Setter of property willTopic .

PySide6.QtMqtt.QMqttClient.state()#
Return type:

ClientState

See also

setState()

Getter of property state .

PySide6.QtMqtt.QMqttClient.stateChanged(state)#
Parameters:

stateClientState

Notification signal of property state .

PySide6.QtMqtt.QMqttClient.subscribe(topic, properties[, qos=0])#
Parameters:
Return type:

PySide6.QtMqtt.QMqttSubscription

Adds a new subscription to receive notifications on topic. The parameter properties specifies additional subscription properties to be validated by the broker. The parameter qos specifies the level at which security messages are received. For more information about the available QoS levels, see Quality of Service .

This function returns a pointer to a QMqttSubscription . If the same topic is subscribed twice, the return value points to the same subscription instance. The MQTT client is the owner of the subscription.

Note

properties will only be passed to the broker when the client specifies MQTT_5_0 as ProtocolVersion .

PySide6.QtMqtt.QMqttClient.subscribe(topic[, qos=0])
Parameters:
Return type:

PySide6.QtMqtt.QMqttSubscription

Adds a new subscription to receive notifications on topic. The parameter qos specifies the level at which security messages are received. For more information about the available QoS levels, see Quality of Service .

This function returns a pointer to a QMqttSubscription . If the same topic is subscribed twice, the return value points to the same subscription instance. The MQTT client is the owner of the subscription.

PySide6.QtMqtt.QMqttClient.transport()#
Return type:

PySide6.QtCore.QIODevice

Returns the transport used for communication with the broker.

See also

setTransport()

PySide6.QtMqtt.QMqttClient.unsubscribe(topic)#
Parameters:

topicPySide6.QtMqtt.QMqttTopicFilter

Unsubscribes from topic. No notifications will be sent to any of the subscriptions made by calling subscribe() .

Note

If a client disconnects from a broker without unsubscribing, the broker will store all messages and publish them on the next reconnect.

PySide6.QtMqtt.QMqttClient.unsubscribe(topic, properties)
Parameters:

Unsubscribes from topic. No notifications will be sent to any of the subscriptions made by calling subscribe() . properties specifies additional user properties to be passed to the broker.

Note

If a client disconnects from a broker without unsubscribing, the broker will store all messages and publish them on the next reconnect.

Note

properties will only be passed to the broker when the client specifies MQTT_5_0 as ProtocolVersion .

PySide6.QtMqtt.QMqttClient.username()#
Return type:

str

See also

setUsername()

Getter of property username .

PySide6.QtMqtt.QMqttClient.usernameChanged(username)#
Parameters:

username – str

Notification signal of property username .

PySide6.QtMqtt.QMqttClient.willMessage()#
Return type:

PySide6.QtCore.QByteArray

See also

setWillMessage()

Getter of property willMessage .

PySide6.QtMqtt.QMqttClient.willMessageChanged(willMessage)#
Parameters:

willMessagePySide6.QtCore.QByteArray

Notification signal of property willMessage .

PySide6.QtMqtt.QMqttClient.willQoS()#
Return type:

int

See also

setWillQoS()

Getter of property willQoS .

PySide6.QtMqtt.QMqttClient.willQoSChanged(willQoS)#
Parameters:

willQoS – int

Notification signal of property willQoS .

PySide6.QtMqtt.QMqttClient.willRetain()#
Return type:

bool

See also

setWillRetain()

Getter of property willRetain .

PySide6.QtMqtt.QMqttClient.willRetainChanged(willRetain)#
Parameters:

willRetain – bool

Notification signal of property willRetain .

PySide6.QtMqtt.QMqttClient.willTopic()#
Return type:

str

See also

setWillTopic()

Getter of property willTopic .

PySide6.QtMqtt.QMqttClient.willTopicChanged(willTopic)#
Parameters:

willTopic – str

Notification signal of property willTopic .