I have got an mqtt consumer that listens to a topic and based on that, I used to send a response on another topic. However now I would like to create a Websocket Secure wss endpoint where i could stream this processed information. Could you tell me if it is possible to do that wth mqttasgi library, if yes how.
Here I leave the code of my consumer.
from mqttasgi.consumers import MqttConsumer
from mqtt_handler.tasks import processmqttmessage
import json
class MyMqttConsumer(MqttConsumer):
async def connect(self):
await self.subscribe('application/+/device/+/event/up', 2)
await self.channel_layer.group_add("stracontech", self.channel_name)
async def receive(self, mqtt_message):
print('Received a message at topic:', mqtt_message['topic'])
print('With payload', mqtt_message['payload'])
print('And QOS:', mqtt_message['qos'])
dictresult = json.loads(mqtt_message['payload'])
jsonresult = json.dumps(dictresult)
processmqttmessage.delay(jsonresult, mqtt_message['topic'])
pass
async def publish_results(self, event):
data = event['result']
await self.publish("stracontech/procesed/"+event['result']['device_id']+"/result", json.dumps(data).encode('utf-8'), qos=2, retain=False)
async def disconnect(self):
await self.unsubscribe('application/+/device/+/event/up')
Pd: @Santiago Ivulich maybe you can help me with that.