channels-asgi-mqtt icon indicating copy to clipboard operation
channels-asgi-mqtt copied to clipboard

An Example would be helpful

Open mustaqeem opened this issue 5 years ago • 6 comments

Hi,

I am trying to integrate it in our project but because of lack of example, I am not able to do it. It would be really helpful if an example integration is committed in this project.

mustaqeem avatar May 02 '19 07:05 mustaqeem

check this pr for an example project

Ali-aqrabawi avatar May 06 '19 22:05 Ali-aqrabawi

@Ali-aqrabawi thanks for the example. Somehow, I was able to consume mqtt message like you did. But I am not sure how to publish mqtt messages. Can you please help me in this?

mustaqeem avatar May 07 '19 06:05 mustaqeem

@mustaqeem not sure if there is a better way but waht you can do is to write to the mqtt channel with message type = mqtt.pub

from asgiref.sync import async_to_sync
from channels.layers import get_channel_layer

msg = {
    "topic": "my_topic",
    "payload": "any payload"
}
channel_layer = get_channel_layer()
async_to_sync(channel_layer.send)(
    "mqtt",
    {
        "type": "mqtt.pub",
        "text": msg
    }
)

btw, did you find a way to receive messages from the channel without running the workers?

Ali-aqrabawi avatar May 07 '19 09:05 Ali-aqrabawi

Thanks @Ali-aqrabawi . I don't think, messages can be received without running workers.

mustaqeem avatar May 07 '19 10:05 mustaqeem

@Ali-aqrabawi , In my project, there are multiple services communicating over mqtt. With your example, I am able to receive message but I am not able to publish message to mqtt that other service can consume.

mustaqeem avatar May 07 '19 11:05 mustaqeem

Dear mustaqeem.I have set up the same project and i have one issues.If you know it pls help me image

nhatnamnguyengtvthcm avatar Jul 07 '20 05:07 nhatnamnguyengtvthcm