Skip to main content

aiomqtt-router

An async router for MQTT topics with aiomqtt.

aiomqtt-router makes subscribing to multiple MQTT topics with aiomqtt much more straightforward and tidier than it otherwise would be. Here's an example from the aiomqtt docs without aiomqtt-router:

import asyncio
import aiomqtt


async def temperature_consumer():
    while True:
        message = await temperature_queue.get()
        print(f"[temperature/#] {message.payload}")


async def humidity_consumer():
    while True:
        message = await humidity_queue.get()
        print(f"[humidity/#] {message.payload}")


temperature_queue = asyncio.Queue()
humidity_queue = asyncio.Queue()


async def distributor(client):
    # Sort messages into the appropriate queues
    async for message in client.messages:
        if message.topic.matches("temperature/#"):
            temperature_queue.put_nowait(message)
        elif message.topic.matches("humidity/#"):
            humidity_queue.put_nowait(message)


async def main():
    async with aiomqtt.Client("test.mosquitto.org") as client:
        await client.subscribe("temperature/#")
        await client.subscribe("humidity/#")
        # Use a task group to manage and await all tasks
        async with asyncio.TaskGroup() as tg:
            tg.create_task(distributor(client))
            tg.create_task(temperature_consumer())
            tg.create_task(humidity_consumer())


if __name__ == "__main__":
    asyncio.run(main())

And here's the same example with aiomqtt-router:

import asyncio
import aiomqtt

from aiomqtt_router import AiomqttRouter

router = AiomqttRouter()


@router.subscribe("humidity/#")
def handle_humidity(message: aiomqtt.Message):
    print(f"[humidity/#] {message.payload}")


@router.subscribe("temperature/#")
async def handle_temperature(message: aiomqtt.Message):
    print(f"[temperature/#] {message.payload}")


async def main():
    async with aiomqtt.Client("test.mosquitto.org") as client:
        await router(client)


if __name__ == "__main__":
    asyncio.run(main())

Release files for aiomqtt-router 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aiomqtt-router 0.1.0
File Size Uploaded
aiomqtt_router-0.1.0.tar.gz 2.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aiomqtt-router 0.1.0
File Interpreter ABI Platform
aiomqtt_router-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 5.4 kB

Release files / aiomqtt_router-0.1.0.tar.gz

Download URL aiomqtt_router-0.1.0.tar.gz
Size 2.5 kB
Tags Source
SHA-256 checksum
How to use checksums
f9b2681ef08d41eda49a52f431729080ff7fbbc6417fb0f239f200adcafcfc16
BLAKE2b-256 checksum
How to use checksums
76f717758557964c71dd31eb52b95abd12d701c2b9ff3c242a27b9988d7691a0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.15.3 CPython/3.11.7 Darwin/23.2.0

Release files / aiomqtt_router-0.1.0-py3-none-any.whl

Download URL aiomqtt_router-0.1.0-py3-none-any.whl
Size 2.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e6625592baef3613bba2f93aa55b64a84345d58f1d699ad45044387e7b4e97d9
BLAKE2b-256 checksum
How to use checksums
1e267a743f6358dfdd162a3242fcd0fa847b4ca9117ddecc28adedd4dd8a203a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.15.3 CPython/3.11.7 Darwin/23.2.0

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page