Server
A server connects multiple YDoc
through a WebsocketServer.
Here is a code example using the websockets library:
import asyncio
from websockets import serve
from ypy_websocket import WebsocketServer
async def server():
async with (
WebsocketServer() as websocket_server,
serve(websocket_server.serve, "localhost", 1234),
):
await asyncio.Future() # run forever
asyncio.run(server())
# main.py
import asyncio
import uvicorn
from ypy_websocket import ASGIServer, WebsocketServer
websocket_server = WebsocketServer()
app = ASGIServer(websocket_server)
async def main():
config = uvicorn.Config("main:app", port=5000, log_level="info")
server = uvicorn.Server(config)
async with websocket_server:
task = asyncio.create_task(server.serve())
while not server.started:
await asyncio.sleep(0)
await asyncio.Future() # run forever
asyncio.run(main())