Thanks a lot. Somehow it did not occur to me that this is the same problem as a group chat ...
On Mon, Mar 18, 2019 at 7:10 PM Aldian Fazrihady <[email protected]> wrote: > > On Mon, Mar 18, 2019 at 9:04 PM Suresh Jeevanandam <[email protected]> > wrote: > >> Can you give some pointers on how you do that? In particular, will the >> client be notified on the same web socket it established with Django server? >> > > When the JS client established the Web Socket connection, you create a > channel group for it: > > from asgiref.sync import async_to_sync > from channels.generic.websocket import JsonWebsocketConsumer > > class MessageConsumer(JsonWebsocketConsumer): > def accept(self): > scope_user = self.scope['user'] > > async_to_sync(self.channel_layer.group_add)("user-" + > scope_user.username, self.channel_name) > super().accept() > > > Your celery task will do this: > > import asyncio > import json > import websockets > from celery import shared_task > from django.conf import settings > > async def async_send_message( > url, message, to_user, message_type="system.message" > ): > async with websockets.connect(url) as websocket: > payload = { > "type": message_type, "message": message, "to": {"user": to_user} > } > await websocket.send(json.dumps(payload)) > > @shared_task > def send_text(text, to_user): > asyncio.get_event_loop().run_until_complete( > messaging_utils.async_send_message( > settings.CHANNEL_MESSAGING_URL, text,username > ) > ) > > > Inside the same MessageConsumer class above, define the methods that > handles message from celery tasks, to be sent back to JS: > > ... > > class MessageConsumer(JsonWebsocketConsumer): > ... > > def receive_json(self, content, **kwargs): > > async_to_sync(self.channel_layer.group_send)("user-" + > content["to"]["user"], content) > > def system_message(self, content): > self.send_json(content) > > > Good Luck! > > Aldian Fazrihady > https://www.aldianfazrihady.com > >> -- >> 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 [email protected]. >> To post to this group, send email to [email protected]. >> 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/CAExkCEU1ULeqVUShjnQzc%2Bi5rChVRBe9H2m3d0eojQ1_8Lbc7Q%40mail.gmail.com >> <https://groups.google.com/d/msgid/django-users/CAExkCEU1ULeqVUShjnQzc%2Bi5rChVRBe9H2m3d0eojQ1_8Lbc7Q%40mail.gmail.com?utm_medium=email&utm_source=footer> >> . >> For more options, visit https://groups.google.com/d/optout. >> > -- > 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 [email protected]. > To post to this group, send email to [email protected]. > 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/CAN7EoAYxoQv_5nUo-vVkSPzj05Rn_SB-E5rTOKj0ss-bzvDLqw%40mail.gmail.com > <https://groups.google.com/d/msgid/django-users/CAN7EoAYxoQv_5nUo-vVkSPzj05Rn_SB-E5rTOKj0ss-bzvDLqw%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- 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 [email protected]. To post to this group, send email to [email protected]. 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/CAExkCEU_oJBL-pcuq97AgyJnZcHv5LO4%3DC-BeEcX_Gx02D9DoQ%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.

