Skip to main content

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

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)

Source distribution for philiprehberger-mqtt-client 0.3.0
File Size Uploaded
philiprehberger_mqtt_client-0.3.0.tar.gz 182.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for philiprehberger-mqtt-client 0.3.0
File Interpreter ABI Platform
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

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page