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)
Batch publishing
Publish multiple messages with shared QoS and retain flags in a single call.
client.publish_many(
[
("home/lights/living", "on"),
("home/lights/kitchen", "off"),
("home/lights/bedroom", "on"),
],
qos=1,
retain=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 |
.publish_many(messages, qos=0, retain=False) |
Publish multiple (topic, payload) tuples with shared QoS and retain flags |
.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 project useful:
License
Release files for philiprehberger-mqtt-client 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| philiprehberger_mqtt_client-0.3.0.tar.gz | 182.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| philiprehberger_mqtt_client-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 189.0 kB
Release files / philiprehberger_mqtt_client-0.3.0.tar.gz
| Download URL | philiprehberger_mqtt_client-0.3.0.tar.gz |
|---|---|
| Size | 182.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4f5b18c206e66793f8cb36902986d9b0ade4dd40d2cd6088e87c873cc2facb71
|
|
BLAKE2b-256 checksum How to use checksums |
3ec81cc5e70eecc1aa2ad3f973368f04a8a74c01d2658d67ad311b433b60876a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.13
|
Release files / philiprehberger_mqtt_client-0.3.0-py3-none-any.whl
| Download URL | philiprehberger_mqtt_client-0.3.0-py3-none-any.whl |
|---|---|
| Size | 6.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ece4a77d24c2b157368d783acd82aadba543b1569f5ff1ae903fe3c98fa66c1d
|
|
BLAKE2b-256 checksum How to use checksums |
6d9c6f1d568ccdd178601aa587e5da5456b51e0afa2c42be29b40dcaef497394
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.13
|