Tuesday, June 19, 2018

Re: channels 2 message type handler in different consumer

Here, relevant code:
1 consumer (want it to receive 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),
},
)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7bc78c23-ea90-4d99-bb74-2884d3a45852%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment