Open ionescu77 opened 3 years ago
It is possible to send message to your consumer directly from your separated script via: https://channels.readthedocs.io/en/latest/topics/channel_layers.html#using-outside-of-consumers
When the new client connects to your consumer inside SteerConsumer
you have self.channel_name
which is unique for that client. To send message to that consumer you just have to execute (in your example from separated script):
from channels.layers import get_channel_layer
channel_layer = get_channel_layer()
# "channel_name" should be replaced for the proper name of course
channel_layer.send("channel_name", {
"type": "chat.message",
"text": "Hello there!",
})
and add inside your SteerConsumer
method to handle this message:
def chat_message(self, event):
# Handles the "chat.message" event when it's sent to us.
self.send(text_data=event["text"])
https://stackoverflow.com/questions/58832526/is-it-possible-to-use-zeromq-sockets-in-a-django-channels-consumer
I've got a hobby project of building an autonomous boat. I now built a GUI using a Vuejs frontend and a Django backend. In this GUI I can see the boat on a map, and send commands to it. Those commands are sent over ZeroMQ sockets which works great.
I'm using Django channels to send the commands from the frontend over a websocket to the backend and from there I send it on over the ZeroMQ socket. My consumer (which works great) looks as follows:
Next to this I also receive location information from the boat over a ZeroMQ socket which I save to the database. I'm running this in a separate script and the frontend simply polls the backend every 2 seconds for updates. Here's the script receiving the boat info:
I would now like to add this
location_socket
to theConsumer
so that theConsumer
can also receive the boat location on the ZeroMQ socket and send it to the frontend over the websocket.I can of course simply add the
location_socket
to theConsumer
its__init__()
method as follows:But I obviously cannot include the
while True
loop in theConsumer
. So from here I'm not sure what to do. I actually don't know whether this is even possible, since Django Channels seems to specifically been made for websockets. I guess I could start using multithreading or multiprocessing libraries, but that is uncharted territory for me.Does anybody know whether and how it is possible to make a ZeroMQ listener in a Django Channel?