{"id":13424729,"url":"https://github.com/sabuhish/fastapi-mqtt","last_synced_at":"2025-05-16T19:03:23.508Z","repository":{"id":38146034,"uuid":"314475975","full_name":"sabuhish/fastapi-mqtt","owner":"sabuhish","description":"fastapi-mqtt is extension for MQTT protocol","archived":false,"fork":false,"pushed_at":"2024-05-22T08:58:45.000Z","size":853,"stargazers_count":278,"open_issues_count":3,"forks_count":43,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-05-12T18:00:54.532Z","etag":null,"topics":["asynchronous","fastapi","fastapimqtt","mqtt","python"],"latest_commit_sha":null,"homepage":"https://sabuhish.github.io/fastapi-mqtt/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sabuhish.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-11-20T07:16:24.000Z","updated_at":"2025-05-06T17:20:37.000Z","dependencies_parsed_at":"2023-02-18T23:30:41.270Z","dependency_job_id":"5d1286f7-c6c2-497d-b6dc-db6efccba980","html_url":"https://github.com/sabuhish/fastapi-mqtt","commit_stats":{"total_commits":81,"total_committers":14,"mean_commits":5.785714285714286,"dds":0.3827160493827161,"last_synced_commit":"856a0027ed232ad92f87c78024cf3022e469b0a3"},"previous_names":[],"tags_count":29,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sabuhish%2Ffastapi-mqtt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sabuhish%2Ffastapi-mqtt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sabuhish%2Ffastapi-mqtt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sabuhish%2Ffastapi-mqtt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sabuhish","download_url":"https://codeload.github.com/sabuhish/fastapi-mqtt/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254592367,"owners_count":22097010,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asynchronous","fastapi","fastapimqtt","mqtt","python"],"created_at":"2024-07-31T00:00:58.454Z","updated_at":"2025-05-16T19:03:23.452Z","avatar_url":"https://github.com/sabuhish.png","language":"Python","funding_links":[],"categories":["Third-Party Extensions","Python"],"sub_categories":["Utils"],"readme":"# fastapi-mqtt\n\nMQTT is a lightweight publish/subscribe messaging protocol designed for M2M (machine to machine) telemetry in low bandwidth environments.\nFastapi-mqtt is the client for working with MQTT.\n\nFor more information about MQTT, please refer to here: [MQTT](MQTT.md)\n\nFastapi-mqtt wraps around [gmqtt](https://github.com/wialon/gmqtt) module. Gmqtt Python async client for MQTT client implementation.\nModule has support of MQTT version 5.0 protocol\n\n[![MIT licensed](https://img.shields.io/github/license/sabuhish/fastapi-mqtt)](https://raw.githubusercontent.com/sabuhish/fastapi-mqtt/master/LICENSE)\n[![GitHub stars](https://img.shields.io/github/stars/sabuhish/fastapi-mqtt.svg)](https://github.com/sabuhish/fastapi-mqtt/stargazers)\n[![GitHub forks](https://img.shields.io/github/forks/sabuhish/fastapi-mqtt.svg)](https://github.com/sabuhish/fastapi-mqtt/network)\n[![GitHub issues](https://img.shields.io/github/issues-raw/sabuhish/fastapi-mqtt)](https://github.com/sabuhish/fastapi-mqtt/issues)\n[![Downloads](https://pepy.tech/badge/fastapi-mqtt)](https://pepy.tech/project/fastapi-mqtt)\n\n---\n\n## **Documentation**: [FastApi-MQTT](https://sabuhish.github.io/fastapi-mqtt/)\n\nThe key feature are:\n\nMQTT specification avaliable with help decarator methods using callbacks:\n\n- on_connect()\n- on_disconnect()\n- on_subscribe()\n- on_message()\n- subscribe(topic)\n\n- MQTT Settings available with `pydantic` class\n- Authentication to broker with credentials\n- unsubscribe certain topics and publish to certain topics\n\n### 🔨 Installation\n\n```sh\npip install fastapi-mqtt\n```\n\n### 🕹 Guide\n\n```python\nfrom contextlib import asynccontextmanager\nfrom typing import Any\n\nfrom fastapi import FastAPI\nfrom gmqtt import Client as MQTTClient\n\nfrom fastapi_mqtt import FastMQTT, MQTTConfig\n\nmqtt_config = MQTTConfig()\nfast_mqtt = FastMQTT(config=mqtt_config)\n\n\n@asynccontextmanager\nasync def _lifespan(_app: FastAPI):\n    await fast_mqtt.mqtt_startup()\n    yield\n    await fast_mqtt.mqtt_shutdown()\n\n\napp = FastAPI(lifespan=_lifespan)\n\n\n@fast_mqtt.on_connect()\ndef connect(client: MQTTClient, flags: int, rc: int, properties: Any):\n    client.subscribe(\"/mqtt\")  # subscribing mqtt topic\n    print(\"Connected: \", client, flags, rc, properties)\n\n@fast_mqtt.subscribe(\"mqtt/+/temperature\", \"mqtt/+/humidity\", qos=1)\nasync def home_message(client: MQTTClient, topic: str, payload: bytes, qos: int, properties: Any):\n    print(\"temperature/humidity: \", topic, payload.decode(), qos, properties)\n\n@fast_mqtt.on_message()\nasync def message(client: MQTTClient, topic: str, payload: bytes, qos: int, properties: Any):\n    print(\"Received message: \", topic, payload.decode(), qos, properties)\n\n@fast_mqtt.subscribe(\"my/mqtt/topic/#\", qos=2)\nasync def message_to_topic_with_high_qos(\n    client: MQTTClient, topic: str, payload: bytes, qos: int, properties: Any\n):\n    print(\n        \"Received message to specific topic and QoS=2: \", topic, payload.decode(), qos, properties\n    )\n\n@fast_mqtt.on_disconnect()\ndef disconnect(client: MQTTClient, packet, exc=None):\n    print(\"Disconnected\")\n\n@fast_mqtt.on_subscribe()\ndef subscribe(client: MQTTClient, mid: int, qos: int, properties: Any):\n    print(\"subscribed\", client, mid, qos, properties)\n\n@app.get(\"/test\")\nasync def func():\n    fast_mqtt.publish(\"/mqtt\", \"Hello from Fastapi\")  # publishing mqtt topic\n    return {\"result\": True, \"message\": \"Published\"}\n```\n\nPublish method:\n\n```python\nasync def func():\n    fast_mqtt.publish(\"/mqtt\", \"Hello from Fastapi\")  # publishing mqtt topic\n    return {\"result\": True, \"message\": \"Published\"}\n```\n\nSubscribe method:\n\n```python\n@fast_mqtt.on_connect()\ndef connect(client, flags, rc, properties):\n    client.subscribe(\"/mqtt\")  # subscribing mqtt topic\n    print(\"Connected: \", client, flags, rc, properties)\n```\n\nChanging connection params\n\n```python\nmqtt_config = MQTTConfig(\n    host=\"mqtt.mosquito.org\",\n    port=1883,\n    keepalive=60,\n    username=\"username\",\n    password=\"strong_password\",\n)\nfast_mqtt = FastMQTT(config=mqtt_config)\n```\n\n### ✅ Testing\n\n- Clone the repository and install it with [`poetry`](https://python-poetry.org).\n- Run tests with `pytest`, using an external MQTT broker to connect (defaults to 'test.mosquitto.org').\n- Explore the fastapi app **examples** and run them with uvicorn\n\n```sh\n# (opc) Run a local mosquitto MQTT broker with docker\ndocker run -d --name mosquitto -p 9001:9001 -p 1883:1883 eclipse-mosquitto:1.6.15\n# Set host for test broker when running pytest\nTEST_BROKER_HOST=localhost pytest\n# Run the example apps against local broker, with uvicorn\nTEST_BROKER_HOST=localhost uvicorn examples.app:app --port 8000 --reload\nTEST_BROKER_HOST=localhost uvicorn examples.ws_app.app:application --port 8000 --reload\n```\n\n# Contributing\n\nFell free to open issue and send pull request.\n\nThanks To [Contributors](https://github.com/sabuhish/fastapi-mqtt/graphs/contributors).\nContributions of any kind are welcome!\n\nBefore you start please read [CONTRIBUTING](https://github.com/sabuhish/fastapi-mqtt/blob/master/CONTRIBUTING.md)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsabuhish%2Ffastapi-mqtt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsabuhish%2Ffastapi-mqtt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsabuhish%2Ffastapi-mqtt/lists"}