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)

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 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.2.1.tar.gz (6.3 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.2.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_mqtt_client-0.2.1.tar.gz
Algorithm Hash digest
SHA256 648836ae0272d07b15d3fb4fa67d7a5d8476dd874611ea8e879885de1815d796
MD5 903720ff36d9f0a67b979b7e52a58661
BLAKE2b-256 7e60951e753e9973baac36ebeb9e1f859ae19ea4a0be99611c4202a7ab97f856

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_mqtt_client-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1adba6116c82c717154e8b79ad94c192338c55db1c6bb400d7b8b7a310ba35f8
MD5 66a70b13dd8650f936b0d5016e128df7
BLAKE2b-256 6fdde25873919bafc234d835b86b548a806b15137b235d776226f6c74a668868

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