Conversation
|
Another possibility would be: async def handle(message, client, *args): # automatically extract wildcards into *args
print(message.payload)
async with aiomqtt.Client(
"test.mosquitto.org",
handlers={"humidity/+/inside/#": handle}
) as client:
await client.subscribe("humidity/#")
async for message in client.messages:
await client.route(message)
I currently prefer this option. Aside: We should be able to optimize message to handler matching with a trie. |
|
Yet another possibility, assigning handlers to subscriptions: async def handle(message):
print(message.payload)
if isinstance(message, Request):
await message.reply("foo")
async with aiomqtt.Client("test.mosquitto.org") as client:
await client.subscribe("humidity/#", handle)
async for message in client.messages:
await client.route(message)We'd have to potentially duplicate messages internally to forward exactly one message per subscription ID. From spec:
|
This proposes a simpler way to filter messages and structure message handling.
Unsorted points that I considered:
+/#) of the topic filter are automatically available as*argsin the handler functionThe interface looks like this:
Where we can process messages concurrently e.g. like this:
Glad to hear feedback on this 🙂