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
How to deal with mqtt_as and multiple subscriptions?
Re: How to deal with mqtt_as and multiple subscriptions?
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.
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.
Chris
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: How to deal with mqtt_as and multiple subscriptions?
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
Then on your PC publish to both - I modified the pubtest script:
The callback runs as expected:
Code: Select all
async def conn_han(client):
await client.subscribe('foo_topic', 1)
await client.subscribe('bar_topic', 1)
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
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
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: How to deal with mqtt_as and multiple subscriptions?
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?
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)
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: How to deal with mqtt_as and multiple subscriptions?
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".
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".
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: How to deal with mqtt_as and multiple subscriptions?
This looks like I was searching for. I will check out this tutorial. Thank you.