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 (soon)...

pip install python-mqtt-framework

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

Defining topic handlers

Within your_app/topic_handlers.py module, you'll write the topic handlers:

from mqtt_framework import TopicHandler
from rest_framework import serializers
from django.core.cache import cache
from pydantic import BaseModel


class SimpleTestModel(BaseModel):
    testing: str


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

    def create(self, validated_data):
        cache.set('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

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

Uploaded Source

Built Distribution

python_mqtt_framework-0.0.2-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: python_mqtt_framework-0.0.2.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.2.tar.gz
Algorithm Hash digest
SHA256 1156f553755aa125e2a91974be117c48c05b0538afa1add2525479ffb567f659
MD5 b0e047c487b87c4d1d5193ec2550db26
BLAKE2b-256 9c8a3295b839fa52e9e8396b8388d699013bb29617bb770f4b33b0c856bb5061

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_mqtt_framework-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f269d56097f04343d522449d2eaf96bac762db22ef12e05f78fc2c699169a001
MD5 927e7f8f4796808735f091ccc662dd0b
BLAKE2b-256 00e2a4558081650238b174724ed29115630ca22f54b271026ae6ae1cf2bbed5f

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