Skip to main content

MQTT support for the Trio async runtime using Paho (update fork)

Project description

trio_paho_mqtt

trio specific async MQTT client. The goal of this project is to have all the MQTT protocol stuff handled by paho-mqtt, while the async loopy stuff is handled by trio. This keeps our MQTT communication async, without having to shove paho-mqtt into a thread, and without having to reimplement the MQTT protocol.

All callbacks are also removed to be more trio-like. Messages are accessed through an async for loop.

This is meant to be a light-as-possible wrapper around paho-mqtt. That way if something goes wrong with MQTT message parsing, it's not my fault ;-)

Usage

First, see the example.py and paho-mqtt documentation. The goal is to expose all the same methods as the paho-mqtt Client. If any methods (besides callbacks) are missing or do something unexpected, this is a bug. Please open an issue.

AsyncClient is initialized with a sync mqtt.Client and a trio.Nursery but then otherwise should operate much like the sync Client. Methods are not coroutines, they are non-blocking functions, so you do not need to call await. The only exception is accessing messages through async for.

Example

import paho.mqtt.client as mqtt
import trio
from trio_paho_mqtt import AsyncClient


async def handle_messages(client):
    async for msg in client.messages():
        print(f"Received msg: {msg}")

async with trio.open_nursery() as nursery:
    sync_client = mqtt.Client()
    client = AsyncClient(sync_client, nursery)
    client.connect('mqtt.eclipse.org', 1883, 60)
    client.subscribe(topic)
    client.publish(topic, msg)

    nursery.start_soon(handle_messages, client)

Related projects

  • gmqtt - Uses asyncio and callbacks. Isn't compatible with trio.
  • hbmqtt - Uses asyncio. Reimplements the MQTT protocol.
  • aiomqtt - Uses asyncio and wraps paho, but still uses the loop from paho-mqtt. I believe all operations are put into a worker thread.
  • distmqtt - anyio-ified hbmqtt fork. Works with trio.
  • asyncio-mqtt - Same idea as this lib (a light wrapper for paho mqtt), but for asyncio.

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

trio-paho-mqtt-up1-0.4.0.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

trio_paho_mqtt_up1-0.4.0-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file trio-paho-mqtt-up1-0.4.0.tar.gz.

File metadata

  • Download URL: trio-paho-mqtt-up1-0.4.0.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.0b2 CPython/3.10.4 Linux/5.17.0-1015-oem

File hashes

Hashes for trio-paho-mqtt-up1-0.4.0.tar.gz
Algorithm Hash digest
SHA256 90ac81a0395fdff44ef7cdcdac625c515aab69d0c8fba311c926535fe905e7dc
MD5 bb1f351cf55427a7ebdbd71d05f56d13
BLAKE2b-256 68cd58e3442387c622677519d2d6e7464e670a7efda59eba0b8a624573bb8c98

See more details on using hashes here.

File details

Details for the file trio_paho_mqtt_up1-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: trio_paho_mqtt_up1-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.0b2 CPython/3.10.4 Linux/5.17.0-1015-oem

File hashes

Hashes for trio_paho_mqtt_up1-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1fdbb82d9826efd41f92ab251d6f34a97d7a3fe4c3d136195ffba5480a858813
MD5 63e0089b2f313d73306ae9332a1e98c3
BLAKE2b-256 240ad40cc4f95568ed1bb70f166269c8b24a7fd59e6879235190d46922124420

See more details on using hashes here.

Supported by

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