channels 2 message type handler in different consumer

19 views
Skip to first unread message

ILia Hopa

unread,
Jun 19, 2018, 11:21:31 AM6/19/18
to Django users
hello! is it possible to set the handler of the group message in the different consumer (from the one from which message was dispatched).

ex: I have 2 consumers: `cli0consumer` and `cli1consumer`. there is a method in a class called from `cli0consumer` which is sending a message to the group 'group0' with the type 'msg.action'. if the handler 'msg_action' is in 'cli1consumer' - its not called. however I want to pass it to client connected to 'cli1consumer'.

how can I solve it?

itsnate_b

unread,
Jun 19, 2018, 2:43:02 PM6/19/18
to Django users
Post some code please and I can try to help.
Message has been deleted
Message has been deleted

ILia Hopa

unread,
Jun 19, 2018, 4:01:32 PM6/19/18
to Django users
Here, relevant code:
Want a clients connected to Cli0Consumer to receive a message.
class Cli0Consumer(JsonWebsocketConsumer):
def connect(self):
try:
async_to_sync(self.channel_layer.group_add)("cli0-group", "custom_channel_name")
except Exception as e:
logging.error(sys.exc_info()[0])

self.accept()

def command_status_all(self, event):
logger.info(event)
self.send_json(event["message"])


class Cli1Consumer(JsonWebsocketConsumer):
def connect(self):
self.accept()
async_to_sync(self.channel_layer.group_add)(_group_name("cli1-group", self.channel_name)
#async_to_sync(self.channel_layer.group_add)(_group_name("cli0-group", self.channel_name) - tried with and without subscribing to cli0-group

def receive_json(self, content, **kwargs):
group_name = _group_name(self.scope['user'], 'web')
logger.info("received: {}".format(content))

with SomeService("cli1-group") as monitor:
monitor.watch_message(content=content)


class SomeService(object):
def __enter__(self):
return self


def __init__(self, group_name):
self.cli1_group = group_name
self.cli0_group = group_name.replace('cli1', 'cli0')

def watch_message(self, content):
async_to_sync(channel_layer.group_send)(
self.cli0_group,
{
"type": "command.status.all",
"message": jsonpickle.encode(msg),
},
)

itsnate_b

unread,
Jun 19, 2018, 4:12:41 PM6/19/18
to Django users
can you please show what's in your routing.py? How are you routing to command_status_all?

ILia Hopa

unread,
Jun 19, 2018, 4:22:30 PM6/19/18
to Django users
i think i found the issue - seems like i can't use a custom channel name.
async_to_sync(self.channel_layer.group_add)("cli0-group", "custom_channel_name")
changing it to
self.channel_name
helped.
Reply all
Reply to author
Forward
0 new messages