gmqtt
raw JSON → 0.7.0 verified Fri May 01 auth: no python
Asynchronous MQTT client for Python (3.5+) using asyncio. Supports QoS 0/1/2, TLS, WebSocket, and MQTT v5.0. Maintained by Wialon. Current version 0.7.0, with irregular releases (last in 2023).
pip install gmqtt Common errors
error TypeError: on_message() takes 4 positional arguments but 5 were given ↓
cause Callback signature expects 4 arguments but gmqtt 0.7.0 passes 5 (added 'properties').
fix
Update callback to
def on_message(client, topic, payload, qos, properties): .... error AttributeError: 'Client' object has no attribute '_on_connect' ↓
cause Trying to monkey-patch internal attribute; gmqtt uses public callback `on_connect` with underscore prefix removed.
fix
Use
client.on_connect = lambda client, flags, rc, properties: ... (5 args). error ModuleNotFoundError: No module named 'gmqtt' ↓
cause Not installed or installed in a different environment.
fix
Run
pip install gmqtt in the correct Python environment. Warnings
breaking In version 0.7.0, `on_message` signature changed: new parameter `properties` added. Old code with `client.on_message = on_message(client, topic, payload, qos)` will raise TypeError. ↓
fix Update callback to accept 5 arguments: client, topic, payload, qos, properties.
gotcha Using `Client` class without calling `set_auth_credentials` when broker requires auth will silently disconnect. No error is raised until publish/subscribe fails. ↓
fix Always call `set_auth_credentials(username, password)` if broker expects authentication.
gotcha Connecting with the same `client_id` to a broker that has session present can cause the previous session to be taken over. The client might not receive messages until reconnection completes. ↓
fix Use unique client IDs or set `clean_session=True` when connecting.
Imports
- Client
from gmqtt import Client - Message
from gmqtt import Message
Quickstart
import asyncio
import os
from gmqtt import Client as MQTTClient
async def main():
client = MQTTClient("client_id")
client.set_auth_credentials(os.environ.get('MQTT_USER', ''), os.environ.get('MQTT_PASS', ''))
# Set callbacks
def on_message(client, topic, payload, qos, properties):
print(f"Received: {topic} -> {payload}")
client.on_message = on_message
await client.connect('localhost', 1883)
client.subscribe('test/topic')
client.publish('test/topic', 'hello')
await asyncio.sleep(2)
await client.disconnect()
if __name__ == '__main__':
asyncio.run(main())