What should I do if I want to send `self.channel_layer.group_send` immediately?

117 views
Skip to first unread message

Kazuki Nagayama

unread,
Jan 12, 2024, 1:19:45 AM1/12/24
to Django users
django = 5..0.1
channels = 4.0.1

I have code like below.

```python
class ChatConsumer(AsyncWebsocketConsumer):
     async def receive(self, text_data):
             await self.channel_layer.group_send(
                 roomId, {"type": "chat_message", "message": "first_chatMessage"}
             )
             import asyncio
             await asyncio.sleep(3)
             await self.channel_layer.group_send(
                 roomId, {"type": "chat_message", "message": "second_chatMessage"}
             )
````

This code expects the first_chatMessage to be sent immediately after `receive()` is called, and the second_chatMessage to be sent 3 seconds later.
But what actually happens is that first_chatMessage and second_chatMessage are sent at the same time after 3 seconds.

When `receive()` completes execution, it appears that the content accumulated in the buffer is sent to the other party.
Is there a function like `flush()` that forces the buffer to be sent?

thank you.

Ryan Nowakowski

unread,
Jan 12, 2024, 4:25:31 PM1/12/24
to django...@googlegroups.com, Kazuki Nagayama
This is not surprising after looking at the code a bit. It looks like group_send calls send which puts the message in a queue that gets drained later during worker.handle.

https://github.com/django/channels/blob/b6dc8c127d7bda3f5e5ae205332b1388818540c5/channels/layers.py#L217

Kazuki Nagayama

unread,
Jan 14, 2024, 8:26:12 PM1/14/24
to Django users

That's exactly what!
Do you know where this queue is used?
I want to forcibly perform the actual operation.

WebSocket is not tied to one request per request, so I want to be able to get 1 request and 2 response.
2024年1月13日土曜日 6:25:31 UTC+9 Ryan Nowakowski:

sachin shende

unread,
Jan 15, 2024, 9:11:08 AM1/15/24
to django...@googlegroups.com
By using self.send() after the first group_send, you ensure that the message is immediately sent, and then the subsequent messages are sent based on time.sleep...
So before import asyncio ,u can write this:

self.send(text_data=json.dumps({"type": "chat_message", "message": "first_chatMessage"}))

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/35f01d4e-4a2e-4f5b-b39a-18c4dc1994a3n%40googlegroups.com.

Kazuki Nagayama

unread,
Jan 15, 2024, 6:58:15 PM1/15/24
to Django users
oh! Good information. thank you. You can still do most of the movements.
However, with this, it will only be sent to me. Is it not possible to do something like group_send?
Should I repeat self.send over and over again? I looked at the internal implementation of group_send, but I couldn't figure out how to do it.

2024年1月15日月曜日 23:11:08 UTC+9 sachin shende:
Reply all
Reply to author
Forward
0 new messages