fastapi-mqtt is extension for MQTT protocol
Project description
fastapi-mqtt
MQTT is a lightweight publish/subscribe messaging protocol designed for M2M (machine to machine) telemetry in low bandwidth environments. Fastapi-mqtt is the client for working with MQTT.
For more information about MQTT, please refer to here: MQTT
Fatapi-mqtt wraps around gmqtt module. Gmqtt Python async client for MQTT client implementation. Module has support of MQTT version 5.0 protocol
Documentation: FastApi-MQTT
The key feature are:
MQTT specification avaliable with help decarator methods using callbacks:
-
on_connect()
-
on_disconnect()
-
on_subscribe()
-
on_message()
-
Base Settings available with
pydanticclass -
Authetication to broker with credentials
-
unsubscribe certain topics and publish to certain topics
🔨 Installation
$ pip install fastapi-mqtt
🕹 Guide
from fastapi import FastAPI
from fastapi_mqtt import FastMQTT, MQTTConfig
app = FastAPI()
mqtt_config = MQTTConfig()
mqtt = FastMQTT(
config=mqtt_config
)
mqtt.init_app(app)
@mqtt.on_connect()
def connect(client, flags, rc, properties):
mqtt.client.subscribe("/mqtt") #subscribing mqtt topic
print("Connected: ", client, flags, rc, properties)
@mqtt.on_message()
async def message(client, topic, payload, qos, properties):
print("Received message: ",topic, payload.decode(), qos, properties)
@mqtt.on_disconnect()
def disconnect(client, packet, exc=None):
print("Disconnected")
@mqtt.on_subscribe()
def subscribe(client, mid, qos, properties):
print("subscribed", client, mid, qos, properties)
Publish method:
async def func():
await mqtt.publish("/mqtt", "Hello from Fastapi") #publishing mqtt topic
return {"result": True,"message":"Published" }
Subscribe method:
@mqtt.on_connect()
def connect(client, flags, rc, properties):
mqtt.client.subscribe("/mqtt") #subscribing mqtt topic
print("Connected: ", client, flags, rc, properties)
Changing connection params
mqtt_config = MQTTConfig(host = "mqtt.mosquito.org",
port= 1883,
keepalive = 60,
username="username",
password="strong_password")
mqtt = FastMQTT(
config=mqtt_config)
Contributing
Fell free to open issue and send pull request.
Thanks To Contributors. Contributions of any kind are welcome!
Before you start please read CONTRIBUTING
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 fastapi-mqtt-0.3.0.tar.gz.
File metadata
- Download URL: fastapi-mqtt-0.3.0.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.7.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b55fbca2980fabd18b6cd5c1bb516134416d6bb4066d0021426f9e4967a057f2
|
|
| MD5 |
d887f02262940c9982e7134c85a6b5a1
|
|
| BLAKE2b-256 |
6b7753b74a3684420d42bc047574c11a5723ad52e1feec4ecfb7e6e8856d60d0
|
File details
Details for the file fastapi_mqtt-0.3.0-py3-none-any.whl.
File metadata
- Download URL: fastapi_mqtt-0.3.0-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.7.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b978561fb9fc8e4a3ca0236645e921cb0ace22654013c7e0b698c6a6c4a74467
|
|
| MD5 |
d00a11e4d7af6ab7417c485502cbacdd
|
|
| BLAKE2b-256 |
7fc857866e712ba5cba5525dc8e2dd8d84e370ab5bb842d4baaf964fce8fd788
|