Skip to main content

An opinionated framework to handle MQTT communication in Python.

Project description

Python MQTT framework

PyPI - Version PyPI - Python Version codecov


Overview

MQTT framework is a library that provides an opinionated structure for setting up message handlers and publishers for MQTT brokers. It is built on top of the paho-mqtt library.


Requirements

  • Python 3.8+

We highly recommend and only officially support the latest patch release of each Python series.

Installation

Install using pip.

pip install python-mqtt-framework

Defining topic handlers

This is how you write the topic handlers:

from mqtt_framework import TopicHandler
from rest_framework import serializers
from pydantic import BaseModel


class SimpleTestModel(BaseModel):
    testing: str


class SimpleTestSerializer(serializers.Serializer):
    testing = serializers.CharField()

    def create(self, validated_data):
        print('test_message', validated_data)
        return validated_data


class SerializerTopicHandler(TopicHandler):
    topic = 'test/topic'
    serializer_class = SimpleTestSerializer
    qos = 1

    # Calls the serializer's "save" method


class PydanticTopicHandler(TopicHandler):
    topic = 'another/topic'
    pydantic_model = SimpleTestModel
    qos = 0

    def handle(self):
        pydantic_instance = self.get_validated_payload()
        # Do something with the pydantic_instance

There's no need to register the topic handlers, the framework will automatically discover them as long as they are imported.

Django Integration

Add 'mqtt_framework' to your INSTALLED_APPS setting.

INSTALLED_APPS = [
    # ...
    'mqtt_framework',
]

Running the MQTT listener

To run the MQTT listener, you can use the runmqtt management command:

python manage.py runmqtt

Settings

MQTT_FRAMEWORK = {
    'BROKER_URL': 'mqtt://<user>:<password>@<host>:<port>',
    'TOPIC_HANDLERS': 'your_app.topic_handlers',
    'KEEPALIVE': 60,
}

That's it, we're done!

python manage.py runmqtt

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_mqtt_framework-0.0.4.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

python_mqtt_framework-0.0.4-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file python_mqtt_framework-0.0.4.tar.gz.

File metadata

  • Download URL: python_mqtt_framework-0.0.4.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.3

File hashes

Hashes for python_mqtt_framework-0.0.4.tar.gz
Algorithm Hash digest
SHA256 f3410bd6a580f72f3510c77ae1c1ae4f96d65df1799b9d503d565c8f250e6713
MD5 0288b3f7b575c63bee000fb8914c72b9
BLAKE2b-256 b7516e38d57c510772d4b4e7ba7639d20a4f49db2d95ea9f8d2174c314dab7b7

See more details on using hashes here.

File details

Details for the file python_mqtt_framework-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for python_mqtt_framework-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 a3082d0abaaa0d48d751f31051a87689c00e4aaaedd0cfa8ab383f23811ef298
MD5 c875f2baa1b8242e976ae0fbbe5630b3
BLAKE2b-256 46a037563b2797d7ab8249ed3514b0f9d3ff6e7bb9260d3c72c6dd3fb0b67252

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page