Skip to main content

Yjs sync protocol server for Django channels

Project description

Channels-Yroom

channels-yroom is a Django Channels WebSocket consumer and worker for synchronizing Yjs clients. It implements the network protocol for Yjs doc synchronization and awareness updates and makes them available as Django Channels WebSocket consumer and worker.

Get started

  1. Install:

    pip install channels-yroom

  2. Add channels and channels_yroom to INSTALLED_APPS in your settings.

  3. Set up your WebSocket consumer in consumers.py.

     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
             """
             user = self.scope["user"]
             if not user.is_staff:
                 await self.close()
                 return
    
             await super().connect()
    
  4. 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(),
                }
            ),
        }
    )
    
  5. 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
    

Showcase: text collaboration example

The example folder contains a simple project that uses y-prosemirror to allow for realtime collaboration on rich text.

Run the included Docker compose file to check it out:

docker compose up
# Then visit localhost:8000

How it works

Yjs clients connect via WebSockets 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.

Under the hood, this project uses yroom which is high-level Python binding to a Yjs synchronization and awareness implementation in Rust based on the official Yjs Rust port.

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

channels_yroom-0.0.2.tar.gz (39.0 kB view details)

Uploaded Source

Built Distribution

channels_yroom-0.0.2-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file channels_yroom-0.0.2.tar.gz.

File metadata

  • Download URL: channels_yroom-0.0.2.tar.gz
  • Upload date:
  • Size: 39.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.24.0

File hashes

Hashes for channels_yroom-0.0.2.tar.gz
Algorithm Hash digest
SHA256 d3e2ccd33314ade00e46322c9ac142a1669958ebdf349df6e7b3b03e0f6d760e
MD5 02def64b31aee2047f240799cbdcb14c
BLAKE2b-256 63098062528f95144c9f8a094c6a43758120d2c1de38120d0ee6845bfba5d6a6

See more details on using hashes here.

File details

Details for the file channels_yroom-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for channels_yroom-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c2deef0d64673b86a365b1975c074215d702ca5a3b3b1c203fc00970d8e5c530
MD5 2e3462076c6c6ed9c6630c651bf81ea1
BLAKE2b-256 f13f3a4fbdad1c7bf93d8d55a557b830edfd31bbe53d7430a959d3d2887740d2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page