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.3.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: python_mqtt_framework-0.0.3.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.3.tar.gz
Algorithm Hash digest
SHA256 dc0dd29507e63e14b312031406e77391dbd62d785a6706e86b6bf0a95a6d0b1b
MD5 66f10cb9961fcb0ae43a14dd73527a0d
BLAKE2b-256 9acf28e8aecd51e623de5db136737d912bb168e9e67cd32fe25b03dabd3626a4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_mqtt_framework-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 4eeb334be7bb4a67d292b6b7058d61f4f8440e76beea5372974e7ed6c63a50e8
MD5 963dce3840e227f99ed2c309ffd82cf4
BLAKE2b-256 5107c21a5509bf4845cba1922dbbf116703ebc248ffa1e0a70a6c7d4045720ba

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