fastapi-mqtt is extension for MQTT protocol
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
The key feature are:
MQTT specification avaliable with help decarator methods using callbacks:
on_connect()
on_disconnect()
on_subscribe()
on_message()
subscribe(topic)
MQTT Settings available with pydantic class
Authentication to broker with credentials
unsubscribe certain topics and publish to certain topics
pip install fastapi-mqtt…Publish method:
async def func():
fast_mqtt.publish("/mqtt", "Hello from Fastapi") # publishing mqtt topic
return {"result": True, "message": "Published"}Subscribe method:
@fast_mqtt.on_connect()
def connect(client, flags, rc, properties):
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",
)
fast_mqtt = FastMQTT(config=mqtt_config)poetry.pytest, using an external MQTT broker to connect (defaults to 'test.mosquitto.org').# (opc) Run a local mosquitto MQTT broker with docker
docker run -d --name mosquitto -p 9001:9001 -p 1883:1883 eclipse-mosquitto:1.6.15
# Set host for test broker when running pytest
TEST_BROKER_HOST=localhost pytest
# Run the example apps against local broker, with uvicorn
TEST_BROKER_HOST=localhost uvicorn examples.app:app --port 8000 --reload
TEST_BROKER_HOST=localhost uvicorn examples.ws_app.app:application --port 8000 --reloadFell free to open issue and send pull request.
Thanks To Contributors. Contributions of any kind are welcome!
Before you start please read CONTRIBUTING
No open issues yet, or sync has not completed.