Skip to content

Switch Y-WebSocket server from Node to Python #1039

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,10 @@ services:

y-provider:
user: ${DOCKER_USER:-1000}
build:
build:
context: .
dockerfile: ./src/frontend/servers/y-provider/Dockerfile
target: y-provider
dockerfile: ./src/backend/servers/y-provider/Dockerfile
#target: y-provider
restart: unless-stopped
env_file:
- env.d/development/common
Expand Down
13 changes: 13 additions & 0 deletions src/backend/servers/y-provider/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM python:3.13

USER root
RUN pip install hypercorn
RUN pip install git+https://github.com/davidbrochart/pycrdt-websocket.git@query-params

USER 1000

COPY ./src/backend/servers/y-provider/server.py .
#COPY ./server.py .

CMD python server.py

49 changes: 49 additions & 0 deletions src/backend/servers/y-provider/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import asyncio
from hypercorn import Config
from hypercorn.asyncio import serve
from pycrdt import Channel, Decoder, write_message
from pycrdt.websocket import ASGIServer, WebsocketServer


class MyWebsocket(Channel):
def __init__(self, websocket: Channel):
self._websocket = websocket
self._room_id = websocket.query_params[b"room"][0]

@property
def path(self) -> str:
return self._websocket.path

def __aiter__(self) -> "Channel":
return self

async def __anext__(self) -> bytes:
return await self.recv()

async def send(self, message: bytes) -> None:
my_message = write_message(self._room_id) + message
return await self._websocket.send(my_message)

async def recv(self) -> bytes:
my_message = await self._websocket.recv()
decoder = Decoder(my_message)
decoder.read_message()
message = my_message[decoder.i0:]
return message


class MyWebsocketServer(WebsocketServer):
async def serve(self, websocket: Channel) -> None:
my_websocket = MyWebsocket(websocket)
await super().serve(my_websocket)


async def main():
websocket_server = MyWebsocketServer()
app = ASGIServer(websocket_server)
config = Config()
config.bind = ["0.0.0.0:4444"]
async with websocket_server:
await serve(app, config, mode="asgi")

asyncio.run(main())
Loading