Framework for building easy MQTT applications
Project description
simple-mqtt
Compact Python MQTT wrapper around paho-mqtt. Focus: clear builder pattern and an explicit split between MQTT v3.1.1 and MQTT v5.
Installation
pip install simple-mqtt
Builders
This package exposes two concrete builders:
MQTTBuilderV3(client_id, host)→ builds aMQTTConnectionV3(MQTT v3.1.1)MQTTBuilderV5(client_id, host)→ builds aMQTTConnectionV5(MQTT v5.0)
Both builders provide the same fluent configuration API.
build() creates the connection wrapper and prepares the client.
fast_build() equals build().connect().
Quickstart: minimal setup
Connect, subscribe, print messages.
Identical for v3 and v5.
from simplemqtt import MQTTBuilderV3, QualityOfService as QoS # for v5 swap to MQTTBuilderV5
conn = MQTTBuilderV3(client_id="demo-client", host="localhost").fast_build()
def on_msg(connection, client, userdata, msg):
print(f"[{msg.topic}] {msg.payload!r} retain={msg.retain} qos={msg.qos}")
conn.subscribe("test/topic", on_message=on_msg, qos=QoS.AtLeastOnce)
conn.publish("test/topic", "hello", qos=QoS.AtLeastOnce, retain=False)
conn.close()
Defaults
- Port:
1883, Keepalive:60 - Clean session:
True - For v5:
SessionExpiryInterval= 0 by default (non‑persistent). If you call.persistent_session(True), it is set to 3600 seconds.
Build a connection
1) Minimal (constructor + connect)
from simplemqtt import MQTTBuilderV3 # for v5 swap to MQTTBuilderV5
conn = (
MQTTBuilderV3("client-1", "broker.example.org")
.fast_build() # build().connect()
)
2) With username/password
from simplemqtt import MQTTBuilderV3 # for v5 swap to MQTTBuilderV5
conn = (
MQTTBuilderV3("client-2", "broker.example.org")
.login("user", "password")
.fast_build()
)
3) Port + keepalive + persistent session + auto‑reconnect
from simplemqtt import MQTTBuilderV3 # for v5 swap to MQTTBuilderV5
conn = (
MQTTBuilderV3("client-3", "broker.example.org")
.port(1884)
.keep_alive(120)
.persistent_session(True)
.auto_reconnect(min_delay=1, max_delay=30)
.fast_build()
)
4) Last Will (LWT)
from simplemqtt import MQTTBuilderV3, QualityOfService as QoS # for v5 swap to MQTTBuilderV5
conn = (
MQTTBuilderV3("client-4", "broker.example.org")
.last_will("devices/dev42/availability", payload="offline", qos=QoS.AtLeastOnce, retain=True)
.fast_build()
)
5) Availability topic
from simplemqtt import MQTTBuilderV3 # for v5 swap to MQTTBuilderV5
conn = (
MQTTBuilderV3("client-5", "broker.example.org")
.availability("devices/dev42/availability", payload_online="online", payload_offline="offline")
.fast_build()
)
6) TLS (defaults)
from simplemqtt import MQTTBuilderV3 # for v5 swap to MQTTBuilderV5
conn = (
MQTTBuilderV3("client-6", "broker.example.org")
.tls() # verify certificates using system defaults
.fast_build()
)
7) TLS with custom CA
from simplemqtt import MQTTBuilderV3 # for v5 swap to MQTTBuilderV5
conn = (
MQTTBuilderV3("client-7", "broker.example.org")
.own_tls("/etc/ssl/certs/ca-bundle.pem", allow_insecure=False)
.fast_build()
)
TLS capabilities (current):
- Supported: server TLS with system CAs (
.tls()), server TLS with custom CA bundle (.own_tls(ca_certs=...)), optional hostname skip viaallow_insecure=True.- Not yet wired in the builder: client certificates (
certfile/keyfilemTLS), custom ciphers/TLS versions, WebSockets-specific TLS options. You can still access these via rawpaho-mqttif needed.
Use a connection
Connect
from simplemqtt import MQTTBuilderV3 # for v5 swap to MQTTBuilderV5
conn = MQTTBuilderV3("client-1", "broker.example.org").build()
conn.connect()
# or
conn.connect(blocking=True)
Callbacks (V3)
import logging
from simplemqtt import MQTTBuilderV3
from simplemqtt.mqtt_connections import MQTTConnectionV3
logger = logging.getLogger("Info")
conn = MQTTBuilderV3("client-2", "broker.example.org").fast_build()
def on_connect_v3(connection: MQTTConnectionV3, client, userdata, flags):
connection.publish("say/hello", "hello :)")
def before_disconnect_v3(connection: MQTTConnectionV3):
connection.publish("say/hello", "bye :(")
def on_disconnect_v3(client, userdata, rc):
logger.info("Too late for publishing")
conn.add_on_connect(on_connect_v3)
conn.add_before_disconnect(before_disconnect_v3)
conn.add_on_disconnect(on_disconnect_v3)
Callbacks (V5)
import logging
from simplemqtt import MQTTBuilderV5
from simplemqtt.mqtt_connections import MQTTConnectionV5
logger = logging.getLogger("Info")
conn = MQTTBuilderV5("client-2", "broker.example.org").fast_build()
def on_connect_v5(connection: MQTTConnectionV5, client, userdata, flags, properties):
connection.publish("say/hello", "hello :)")
def before_disconnect_v5(connection: MQTTConnectionV5):
connection.publish("say/hello", "bye :(")
def on_disconnect_v5(client, userdata, rc, properties):
logger.info("Too late for publishing")
conn.add_on_connect(on_connect_v5)
conn.add_before_disconnect(before_disconnect_v5)
conn.add_on_disconnect(on_disconnect_v5)
Subscribe
Identical for v3 and v5.
from simplemqtt import QualityOfService as QoS
def on_msg(connection, client, userdata, msg):
print(msg.topic, msg.payload)
conn.subscribe("sensors/+/temp", on_message=on_msg, qos=QoS.AtLeastOnce)
Unsubscribe
Identical for v3 and v5.
# Remove one or more filters
conn.unsubscribe("sensors/+/temp", "actuators/#")
Close
Identical for v3 and v5.
conn.close() # loop_stop + disconnect
Protocol specifics
MQTT v3.1.1
Publish
from simplemqtt import QualityOfService as QoS
# Simple
conn.publish("demo/topic", "payload")
# With QoS/retain
conn.publish("demo/topic", "payload", qos=QoS.AtLeastOnce, retain=True)
# Wait for publish completion
conn.publish("demo/topic", "payload", qos=QoS.AtLeastOnce, wait_for_publish=True)
Subscribe
from simplemqtt import QualityOfService as QoS
def on_msg_v3(connection, client, userdata, msg):
print("v3:", msg.topic, msg.payload)
conn.subscribe("demo/v3/#", on_message=on_msg_v3, qos=QoS.ExactlyOnce)
MQTT v5
Build a v5 connection
from simplemqtt import MQTTBuilderV5
conn = MQTTBuilderV5("client-5", "broker.example.org").fast_build()
Publish with properties
from paho.mqtt.properties import Properties
from paho.mqtt.packettypes import PacketTypes
from simplemqtt import QualityOfService as QoS
props = Properties(PacketTypes.PUBLISH)
props.MessageExpiryInterval = 30 # seconds
# Simple
conn.publish("demo5/topic", "payload-v5")
# With QoS/retain/properties
conn.publish("demo5/topic", "payload-v5", qos=QoS.AtLeastOnce, retain=False, properties=props)
# Wait for completion
conn.publish("demo5/topic", "payload-v5", qos=QoS.AtLeastOnce, wait_for_publish=True, properties=props)
Subscribe with options
from simplemqtt import QualityOfService as QoS, RetainHandling
def on_msg_v5(connection, client, userdata, msg):
print("v5:", msg.topic, msg.payload, "retain:", msg.retain)
conn.subscribe(
"demo5/#",
on_message=on_msg_v5,
qos=QoS.AtLeastOnce,
no_local=True,
retain_as_published=True,
retain_handling=RetainHandling.SendRetainedOnNewSubscription,
)
Logging
This package uses logging with a NullHandler. Enable it like this:
import logging
logging.basicConfig(level=logging.INFO)
logging.getLogger("simplemqtt").setLevel(logging.DEBUG)
Best practices
- Use a stable
client_idper device. - Set LWT (
.last_will(...)) with QoS ≥ 1 andretain=True. - Enable auto‑reconnect for production.
- For v5, use
retain_handlingandno_localto reduce retained floods and pub/sub loops.
License
MIT (see 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 simple_mqtt-0.1.0.tar.gz.
File metadata
- Download URL: simple_mqtt-0.1.0.tar.gz
- Upload date:
- Size: 10.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
611331251f2b73bbb58b60a927f668bee4262a00f1e8eb98b7e992e1bb45264e
|
|
| MD5 |
18b1e3ef21ab4ea24eb26e1b037ab0f1
|
|
| BLAKE2b-256 |
74db0e799ac207f9e4eb24cc1adb17cf8d7d265467c36ed9b7d5f3647bbfcab7
|
File details
Details for the file simple_mqtt-0.1.0-py3-none-any.whl.
File metadata
- Download URL: simple_mqtt-0.1.0-py3-none-any.whl
- Upload date:
- Size: 15.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
716ad3e4fcec3413a6e548bd5eac894506d4b93f3481dc6226978039e2cdd62c
|
|
| MD5 |
aa190d63d252a6ec39c8dad380913edb
|
|
| BLAKE2b-256 |
7986b1f566ab8414d458d8c22659d912586b2822ba7eda3fa14838fd0df074db
|