Page 1 of 1

How to deal with mqtt_as and multiple subscriptions?

Posted: Wed Jul 06, 2022 6:25 pm
by werner
I want to extend the unclean.py example and added some subscriptions. This is working fine so far. My question is how to proceed with additional subscriptions? Is it possible to have multiple sub_cbs for each or is a message queue the better solution to process the messages?

Thanks in advance, Werner

Re: How to deal with mqtt_as and multiple subscriptions?

Posted: Thu Jul 07, 2022 6:34 am
by ChrisO
I don’t think that’s how mqtt_as was built.
Generally you subscribe with a broad enough topic with the use of wildcard and you let the subs_cb filter the messages based on topic. If you process messages right when they come in, it usually doesn’t take that long, so I’m not sure what you want to use the queue for. Let’s just say that the Queue is not necessary per se and depends on your use case.

Re: How to deal with mqtt_as and multiple subscriptions?

Posted: Thu Jul 07, 2022 7:43 am
by pythoncoder
It is possible to subscribe to multiple topics but there can only be one subscription callback. To demo this, take the script range.py and adapt the connection handler as follows

Code: Select all

async def conn_han(client):
    await client.subscribe('foo_topic', 1)
    await client.subscribe('bar_topic', 1)
Then on your PC publish to both - I modified the pubtest script:

Code: Select all

#! /bin/bash
# mosquitto_sub -h 192.168.0.10 -t result
while :
do
    mosquitto_pub -h 192.168.0.10 -t foo_topic -m "gordon bennett" -q 1
    sleep 5
    mosquitto_pub -h 192.168.0.10 -t bar_topic -m "pete was here" -q 1
    sleep 5
done
The callback runs as expected:

Code: Select all

publish 50
Topic: "foo_topic" Message: "gordon bennett" Retained: False
publish 51
Topic: "bar_topic" Message: "pete was here" Retained: False
publish 52
Topic: "foo_topic" Message: "gordon bennett" Retained: False

Re: How to deal with mqtt_as and multiple subscriptions?

Posted: Thu Jul 07, 2022 8:26 am
by werner
Thank you for the fast response. I found also an starting point to deal with multiple topics and one subscription callback.
The question for me still is not to lose a message while processing another one. Is it possible to run these processes in the background?

Code: Select all

# multiple topics
def callback(topic, msg): 
    print((topic, msg))
    if topic == b"username/feeds/Temperature control":
        sub_cb_temp(topic, msg)
    else
        sub_cb_weight(topic, msg)

Re: How to deal with mqtt_as and multiple subscriptions?

Posted: Thu Jul 07, 2022 8:35 am
by pythoncoder
It might be worth you looking at the uasyncio tutorial as this question is a generic query about asynchronous programming.

In short, the callback should run to completion quickly but it can launch asynchronous tasks using uasyncio.create_task(). These will run "in the background".

Re: How to deal with mqtt_as and multiple subscriptions?

Posted: Thu Jul 07, 2022 9:44 am
by werner
This looks like I was searching for. I will check out this tutorial. Thank you.