Django Channels consumer
Bases: AsyncWebsocketConsumer
A working consumer for Django Channels.
This consumer can be used out of the box simply by adding:
path("ws/<str:room>", YjsConsumer.as_asgi())
urls.py
file. In practice, once you
set up Channels,
you might have something like:
# urls.py
from django.urls import path
from backend.consumer import DocConsumer, UpdateConsumer
urlpatterns = [
path("ws/<str:room>", YjsConsumer.as_asgi()),
]
# asgi.py
import os
from channels.routing import ProtocolTypeRouter, URLRouter
from urls import urlpatterns
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "backend.settings")
application = ProtocolTypeRouter({
"websocket": URLRouter(urlpatterns_ws),
})
Additionally, the consumer can be subclassed to customize its behavior.
In particular,
- Override
make_room_name
to customize the room name. - Override
make_ydoc
to initialize the YDoc. This is useful to initialize it with data from your database, or to add observers to it). - Override
connect
to do custom validation (like auth) on connect, but be sure to callawait super().connect()
in the end. - Call
group_send_message
to send a message to an entire group/room. - Call
send_message
to send a message to a single client, although this is not recommended.
A full example of a custom consumer showcasing all of these options is:
import y_py as Y
from asgiref.sync import async_to_sync
from channels.layers import get_channel_layer
from ypy_websocket.django_channels_consumer import YjsConsumer
from ypy_websocket.yutils import create_update_message
class DocConsumer(YjsConsumer):
def make_room_name(self) -> str:
# modify the room name here
return self.scope["url_route"]["kwargs"]["room"]
async def make_ydoc(self) -> Y.YDoc:
doc = Y.YDoc()
# fill doc with data from DB here
doc.observe_after_transaction(self.on_update_event)
return doc
async def connect(self):
user = self.scope["user"]
if user is None or user.is_anonymous:
await self.close()
return
await super().connect()
def on_update_event(self, event):
# process event here
...
async def doc_update(self, update_wrapper):
update = update_wrapper["update"]
Y.apply_update(self.ydoc, update)
await self.group_send_message(create_update_message(update))
def send_doc_update(room_name, update):
layer = get_channel_layer()
async_to_sync(layer.group_send)(room_name, {"type": "doc_update", "update": update})
Source code in ypy_websocket/django_channels_consumer.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
|
WrappedMessage
Bases: TypedDict
A wrapped message to send to the client.
Source code in ypy_websocket/django_channels_consumer.py
175 176 177 178 |
|
group_send_message(message)
async
Send a message to the group.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message |
bytes
|
The message to send. |
required |
Source code in ypy_websocket/django_channels_consumer.py
188 189 190 191 192 193 194 195 196 |
|
make_room_name()
Make the room name for a new channel.
Override to customize the room name when a channel is created.
Returns:
Type | Description |
---|---|
str
|
The room name for a new channel. Defaults to the room name from the URL route. |
Source code in ypy_websocket/django_channels_consumer.py
130 131 132 133 134 135 136 137 138 |
|
make_ydoc()
async
Make the YDoc for a new channel.
Override to customize the YDoc when a channel is created (useful to initialize it with data from your database, or to add observers to it).
Returns:
Type | Description |
---|---|
YDoc
|
The YDoc for a new channel. Defaults to a new empty YDoc. |
Source code in ypy_websocket/django_channels_consumer.py
140 141 142 143 144 145 146 147 148 149 |
|
send_message(message_wrapper)
async
Send a message to the client.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message_wrapper |
WrappedMessage
|
The message to send, wrapped. |
required |
Source code in ypy_websocket/django_channels_consumer.py
180 181 182 183 184 185 186 |
|