Yjs sync protocol server for Django channels
Project description
Channels-Yroom
Django Channels Websocket consumer and worker for the Yjs network protocol
channels-yroom
implements the network protocol for Yjs doc synchronization and awareness updates and makes them available as a Django Channels Websocket consumer and worker.
Get started
-
Add
channels
andchannels_yroom
toINSTALLED_APPS
in your settings. -
Set up your websocket consumer.
These can be quite simple
from channels_yroom.consumer import YroomConsumer class TextCollaborationConsumer(YroomConsumer): def get_room_group_name(self) -> str: """ Determine a unique name for this room, e.g. based on URL """ room_name = self.scope["url_route"]["kwargs"]["room_name"] return "textcollab_%s" % room_name async def connect(self) -> None: """ Optional: perform some sort of authentication Call .join_room() to connect client to document """ user = self.scope["user"] if not user.is_staff: await self.close() return await super().connect()
-
Hook your websocket patterns in your
asgi.py
and add a"channel"
protocol router for the"yroom"
channel name:# ... application = ProtocolTypeRouter( { "http": get_asgi_application(), "websocket": AllowedHostsOriginValidator( AuthMiddlewareStack(URLRouter(textcollab.routing.websocket_urlpatterns)) ), "channel": ChannelNameRouter( { "yroom": YRoomChannelConsumer.as_asgi(), } ), } )
-
In addition to your webserver with websockets support (e.g. daphne or uvicorn), you need to run a channels worker. You can run the
yroom
worker implementation that supports graceful shutdown:python manage.py yroom
How it works
Yjs clients connect via Websocket to a Channels Websocket Consumer which can perform e.g. authentication and then forwards messages via channel layer to a central worker. This worker runs in a separate process and keeps a Yjs document + awareness information for each 'room', processes synchronization and awareness updates and sends responses (single and broadcast to room) to the Websocket consumers.
Example flow
sequenceDiagram
participant Alice
participant WebsocketConsumerA
participant Yroom Worker
participant WebsocketConsumerB
participant Bob
Alice->>+WebsocketConsumerA: connect
WebsocketConsumerA->>+Yroom Worker: connect
Yroom Worker->>+WebsocketConsumerA: sync1
WebsocketConsumerA->>+Alice: forward sync1
Alice->>+WebsocketConsumerA: sync2
WebsocketConsumerA->>+Yroom Worker: forward sync2
Bob->>WebsocketConsumerB: connect
WebsocketConsumerB->>+Yroom Worker: connect
Yroom Worker->>+WebsocketConsumerB: sync1
WebsocketConsumerB->>+Bob: forward sync1
Bob->>+WebsocketConsumerB: sync2
WebsocketConsumerB->>+Yroom Worker: forward sync2
Bob->>+WebsocketConsumerB: update from Bob
WebsocketConsumerB->>+Yroom Worker: forward update from Bob
par
Yroom Worker->>WebsocketConsumerA: broadcast update
WebsocketConsumerA->>Alice: forward update
and
Yroom Worker->>WebsocketConsumerB: broadcast update
WebsocketConsumerB->>Bob: forward update
end
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file channels_yroom-0.0.1.tar.gz
.
File metadata
- Download URL: channels_yroom-0.0.1.tar.gz
- Upload date:
- Size: 43.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.23.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 08361c4fe4305b05539a509503d325a63fc4addafb679bd85991538af0577219 |
|
MD5 | d9e1f184e119ad0d0adaeaa550d13bb1 |
|
BLAKE2b-256 | 0276ac7b4d17fbe78cdf946c1c4f59d989bc9c26aaf42fe1f4d94c0374f5acb7 |
File details
Details for the file channels_yroom-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: channels_yroom-0.0.1-py3-none-any.whl
- Upload date:
- Size: 11.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.23.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a465f94620d2cb3e104cd054bb5d773815e71149a0abd40f7f0b8993fba73e18 |
|
MD5 | f9829e1c7fc58e5184104f9cd162b6df |
|
BLAKE2b-256 | f9aa70c101586d01da5a46631ff42fbc54433f96a693b4860896c62ad1bc0a60 |