Skip to main content

Simplified MQTT pub/sub wrapper with auto-reconnect

Project description

philiprehberger-mqtt-client

Tests PyPI version Last updated

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:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

philiprehberger_mqtt_client-0.3.0.tar.gz (182.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

philiprehberger_mqtt_client-0.3.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_mqtt_client-0.3.0.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_mqtt_client-0.3.0.tar.gz
Algorithm Hash digest
SHA256 4f5b18c206e66793f8cb36902986d9b0ade4dd40d2cd6088e87c873cc2facb71
MD5 77d2a906eeef567a70395b61e881ec22
BLAKE2b-256 3ec81cc5e70eecc1aa2ad3f973368f04a8a74c01d2658d67ad311b433b60876a

See more details on using hashes here.

File details

Details for the file philiprehberger_mqtt_client-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_mqtt_client-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ece4a77d24c2b157368d783acd82aadba543b1569f5ff1ae903fe3c98fa66c1d
MD5 b08a6f2cdf5ef0e0738d0fb13a47a2fd
BLAKE2b-256 6d9c6f1d568ccdd178601aa587e5da5456b51e0afa2c42be29b40dcaef497394

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page