Basic functions for managing Channels
The basic functions for managing Channels are as follows:
send()
: This is used to send new messages from the Consumer to a single client. We have used this function from the beginning of this book. However, we used theJsonWebsocketConsumer
wrapper to makesend_json()
more convenient for sending JSON:data = { "my_data": "hi", } self.send_json(data)
group_send()
: This is used to send new messages from the Consumer to a group of clients that we have previously defined. It is an asynchronous function, so we will need the whole Consumer to be asynchronous or, preferably, use theasync_to_sync
function. In the following example, you can see how the{"my_data": "hi"}
JSON is sent to the whole group as"Main"
:from asgiref.sync import async_to_sync async_to_sync(self.channel_layer...