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),
},
)