--
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...@googlegroups.com.
To post to this group, send email to django...@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/d6ee181c-8d02-4a51-9215-3da10a75773c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAF%2BYYc5HdBqT5z9ku_PJ-u3Ac6uXBf%2BpTaTxWox1up_rMw%2BVxQ%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAN7EoAaWMRZ0jb530A1qq763vu%2B2fOf4hKR4MVf%3DQ3XusFUFtg%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAF%2BYYc5HdBqT5z9ku_PJ-u3Ac6uXBf%2BpTaTxWox1up_rMw%2BVxQ%40mail.gmail.com.
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?
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()
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
)
)
...
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)
--
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...@googlegroups.com.
To post to this group, send email to django...@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/CAExkCEU1ULeqVUShjnQzc%2Bi5rChVRBe9H2m3d0eojQ1_8Lbc7Q%40mail.gmail.com.
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.