Simplified MQTT pub/sub wrapper with auto-reconnect
Project description
philiprehberger-mqtt-client
Simplified MQTT pub/sub wrapper with auto-reconnect.
Installation
pip install philiprehberger-mqtt-client
Usage
Basic Pub/Sub
from philiprehberger_mqtt_client import MQTTClient
client = MQTTClient("mqtt://localhost:1883", client_id="my-app")
@client.on("home/temperature")
def on_temperature(topic, payload):
print(f"Temperature: {float(payload)}C")
@client.on("home/+/status") # wildcard
def on_device_status(topic, payload):
device = topic.split("/")[1]
print(f"{device}: {payload}")
# Publish
client.publish("home/lights/living", "on")
client.publish_json("home/sensors/data", {"temp": 22.5, "humidity": 45})
# Connect (blocks with auto-reconnect)
client.connect()
# Or background mode
client.connect(background=True)
Offline Message Queue
Messages published while disconnected are automatically queued and sent when the connection is re-established.
client = MQTTClient("mqtt://localhost:1883", offline_queue_size=500)
# These are queued if not yet connected
client.publish("sensors/temp", "22.5")
client.publish_json("sensors/data", {"humidity": 45})
# Check queue status
print(f"Pending messages: {client.pending_count()}")
# Connect — queued messages are flushed automatically
client.connect(background=True)
# Discard queued messages if needed
client.clear_queue()
API
| Function / Class | Description |
|---|---|
MQTTClient(broker_url, client_id, ...) |
Simplified MQTT client with auto-reconnect and offline queue |
.on(topic, qos) |
Decorator to subscribe to a topic (supports MQTT wildcards) |
.subscribe(topic, callback, qos) |
Programmatically subscribe to a topic |
.publish(topic, payload, qos, retain) |
Publish a message (queues if disconnected) |
.publish_json(topic, data, qos, retain) |
Publish a JSON-serialized message |
.connect(background) |
Connect to the broker and start listening |
.disconnect() |
Disconnect from the broker |
.pending_count() |
Return number of messages in the offline queue |
.clear_queue() |
Discard all queued messages |
.is_connected |
Whether the client is currently connected |
Development
pip install -e .
python -m pytest tests/ -v
Support
If you find this package useful, consider giving it a star on GitHub — it helps motivate continued maintenance and development.
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file philiprehberger_mqtt_client-0.2.0.tar.gz.
File metadata
- Download URL: philiprehberger_mqtt_client-0.2.0.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d8843b6968112b9c4f7961222f8eb436e2922488bc6b31e3cace08d50802200e
|
|
| MD5 |
2d139c80321d6f15715da8c011f1d554
|
|
| BLAKE2b-256 |
489a2583fd4fe170ebaac5c85064a3476180c0f4010697be398b64e90060ef72
|
File details
Details for the file philiprehberger_mqtt_client-0.2.0-py3-none-any.whl.
File metadata
- Download URL: philiprehberger_mqtt_client-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
145abc869cdd9e5275c1c9ba2eecf79f0d40eda03d172e2b91358380be29f7e7
|
|
| MD5 |
cb21a289e179f340d88c08a9418c98a1
|
|
| BLAKE2b-256 |
ad770d528d82dcf11b5fd14d4d5c665ea914b07d3842b3c9d23b7981ad0e8238
|