Hi,
i read the article, because i have the same situation. Its not clear for me how to use the receive_many function outside Django. Its only possible to send message
Group('sensor').send({'text': 'Hello, world!'}) from outside and receive it on the browser.
But not to receive a message.
Here my code example:
import asyncio
import traceback
import sys
from channels import Group
from sensor.asgi import channel_layer
class ChannelsConsumer(object):
async def start(self, channel_layer, channel_names):
if isinstance(channel_names, str):
channel_names = [channel_names]
while True:
channel, message = channel_layer.receive_many('sensor',block=False)
# self.stdout.write('RECV' + str(channel) + str(message))
Group('sensor').send({'text': 'Recieved' + str(channel) + str(message)})
if channel:
print('RECV', channel, message)
self.stdout.write('RECV' + str(channel) + str(message))
#
# await some_long_async_operation_here()
#
Group('sensor').send({'text': 'Hello, world!'})
else:
await asyncio.sleep(0.1)
def run(self, channel_layer, channel_names):
loop = asyncio.get_event_loop()
asyncio.ensure_future(self.start(channel_layer, channel_names))
try:
loop.run_forever()
except Exception as e:
traceback.print_exc()
finally:
loop.close()
consumer = ChannelsConsumer()
consumer.run(channel_layer, 'sensor')