Version 5.0 is used by default. If your broker does not support 5.0 protocol version and responds with proper CONNACK reason code, client will downgrade to 3.1 and reconnect automatically. Note, that some brokers just fail to parse the 5.0 format CONNECT packet, so first check manually if your broker handles this properly.
You can also force version in connect method:
Connect properties are passed to Client object as kwargs (later they are stored together with properties received from broker in client.properties field). See example below.
session_expiry_interval - int Session expiry interval in seconds. If the Session Expiry Interval is absent the value 0 is used. If it is set to 0, or is absent, the Session ends when the Network Connection is closed. If the Session Expiry Interval is 0xFFFFFFFF (max possible value), the Session does not expire.
receive_maximum - int The Client uses this value to limit the number of QoS 1 and QoS 2 publications that it is willing to process concurrently.
user_property - tuple(str, str) This property may be used to provide additional diagnostic or other information (key-value pairs).
maximum_packet_size - int The Client uses the Maximum Packet Size (in bytes) to inform the Server that it will not process packets exceeding this limit.
This properties will be also sent in publish packet from broker, they will be passed to on_message callback.
message_expiry_interval - int If present, the value is the lifetime of the Application Message in seconds.
content_type - unicode UTF-8 Encoded String describing the content of the Application Message. The value of the Content Type is defined by the sending and receiving application.
user_property - tuple(str, str)
subscription_identifier - int (see subscribe properties) sent by broker
topic_alias - int First client publishes messages with topic string and kwarg topic_alias. After this initial message client can publish message with empty string topic and same topic_alias kwarg.
subscription_identifier - int If the Client specified a Subscription Identifier for any of the overlapping subscriptions the Server MUST send those Subscription Identifiers in the message which is published as the result of the subscriptions.
Reconnects
By default, connected MQTT client will always try to reconnect in case of lost connections. Number of reconnect attempts is unlimited.
If you want to change this behaviour, do the following:
Code above will set number of reconnect attempts to 10 and delay between reconnect attempts to 1min (60s). By default reconnect_delay=6 and reconnect_retries=-1 which stands for infinity.
The first reconnect after a lost connection waits reconnect_delay; after each failed attempt in a row (refusal, network error, ...)
the delay doubles (starting from 1s if reconnect_delay is 0), up to reconnect_delay_max (300s by default):
client.set_config({'reconnect_delay_max': 120})
Note that manually calling await client.disconnect() will set reconnect_retries for 0, which will stop auto reconnect.
Every failure is retried, broker refusals included: even a refusal that retrying alone cannot fix (bad credentials,
banned client, server moved, ...) may go away, and the backoff keeps the retries cheap. For those
client.permanently_refused turns True and client.connect_error holds the refusal: alert on them.
A failed await client.connect(...) raises the refusal, and auto-reconnect keeps trying meanwhile. Call
await client.disconnect() before retrying connect() yourself or giving up: otherwise that background reconnect
opens a second connection with the same client id.
Rotating credentials
If credentials expire (e.g. tokens), pass a credentials_provider. gmqtt awaits it before every connect and reconnect,
and uses what it returns instead of the credentials set with set_auth_credentials:
async def credentials(client, error):
# error: the refusal (MQTTConnectError) since the provider was last called,
# None otherwise (e.g. after a plain connection loss)
if error is not None and error.code in (134, 135): # bad user name or password, not authorized
await issuer.refresh()
return issuer.token, None
client = MQTTClient("client-id", credentials_provider=credentials)
With a provider, auth refusals (134/135, 4/5 on MQTT 3.1.1) are routine: they do not set permanently_refused.
Asynchronous on_message callback
You can define asynchronous on_message callback.
Note that it must return valid PUBACK code (0 is success code, see full list in constants)