Skip to main content

Idomatic asyncio wrapper around paho-mqtt.

This project has been archived.

The maintainers of this project have marked this project as archived. No new releases are expected.

Project description

license

MQTT client with idiomatic asyncio interface 🙌

Write code like this:

async with Client('test.mosquitto.org') as client:
    await client.subscribe('floors/#')

    async with client.filtered_messages('floors/+/humidity') as messages:
        async for message in messages:
            print(message.decode())
  • No more callbacks! 👍
  • No more return codes (welcome to the MqttError)
  • Graceful disconnection (forget about on_unsubscribe, on_disconnect, etc.)
  • Compatible with async code
  • Did we mention no more callbacks?

asyncio-mqtt combines the stability of the time-proven paho-mqtt library with a modern, asyncio-based interface.

The whole thing is less than 250 lines of code.

Advanced use ⚡

Let's make the example from before more interesting:

import asyncio
from random import randrange
from asyncio_mqtt import Client

async def log_filtered_messages(client, topic_filter):
    async with client.filtered_messages(topic_filter) as messages:
        async for message in messages:
            print(f'[topic_filter="{topic_filter}"]: {message.decode()}')

async def log_unfiltered_messages(client):
    async with client.unfiltered_messages() as messages:
        async for message in messages:
            print(f'[unfiltered]: {message.decode()}')

async def main():
    async with Client('test.mosquitto.org') as client:
        await client.subscribe('floors/#')

        # You can create any number of message filters
        asyncio.create_task(log_filtered_messages(client, 'floors/+/humidity'))
        asyncio.create_task(log_filtered_messages(client, 'floors/rooftop/#'))
        # 👉 Try to add more filters!

        # All messages that doesn't match a filter will get logged here
        asyncio.create_task(log_unfiltered_messages(client))

        # Publish a random value to each of these topics
        topics = [
            'floors/basement/humidity',
            'floors/rooftop/humidity',
            'floors/rooftop/illuminance',
            # 👉 Try to add more topics!
        ]
        while True:
            for topic in topics:
                message = randrange(100)
                print(f'[topic="{topic}"] Publishing message={message}')
                await client.publish(topic, message, qos=1)
                await asyncio.sleep(2)

asyncio.run(main())

Alternative asyncio-based MQTT clients:

Is asyncio-mqtt not what you are looking for? Try another client:

This is not an exhaustive list.

Dependencies

There is only a single dependency:

License

license

Note that the underlying paho-mqtt library is dual-licensed. One of the licenses is the so-called Eclipse Distribution License v1.0. It is almost word-for-word identical to the BSD 3-clause License. The only differences are:

  • One use of "COPYRIGHT OWNER" (EDL) instead of "COPYRIGHT HOLDER" (BSD)
  • One use of "Eclipse Foundation, Inc." (EDL) instead of "copyright holder" (bsd)

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

asyncio_mqtt-0.1.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

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

asyncio_mqtt-0.1.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file asyncio_mqtt-0.1.0.tar.gz.

File metadata

  • Download URL: asyncio_mqtt-0.1.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.0

File hashes

Hashes for asyncio_mqtt-0.1.0.tar.gz
Algorithm Hash digest
SHA256 04f1d4e9ee184cffe1953b2ce972e1cdb8fcab5fa56c7484956c1539b908b5a7
MD5 28c90b524a20f5e8b7040b9c803f7e46
BLAKE2b-256 c3238dd726d7abb7b9965e4f4a45a05aeea1d9c16729a51881aa12b052bdefbf

See more details on using hashes here.

File details

Details for the file asyncio_mqtt-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: asyncio_mqtt-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.0

File hashes

Hashes for asyncio_mqtt-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 169f3ccf4c9d207f00f0b68a2de4fb076296fc9863effac2154a50e724759ef4
MD5 70c9634dbd6e4a130b0b9199a6c77b19
BLAKE2b-256 2d1b31543713218f4359492cdcbd5ee6d8170fb92597e2764fb68f2c798885d7

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