[Django]-Is it possible to use ZeroMQ sockets in a Django Channels Consumer?

2👍

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"])

Leave a comment