WebSocket connector for Ypy
Project description
ypy-websocket
ypy-websocket is an ASGI-compatible async WebSocket connector for Ypy.
Usage
Client
Here is a code example:
import asyncio
import y_py as Y
from websockets import connect
from ypy_websocket import WebsocketProvider
async def client():
ydoc = Y.YDoc()
async with connect("ws://localhost:1234/my-roomname") as websocket:
WebsocketProvider(ydoc, websocket)
ymap = ydoc.get_map("map")
with ydoc.begin_transaction() as t:
ymap.set(t, "key", "value")
asyncio.run(client())
Server
Here is a code example:
import asyncio
from websockets import serve
from ypy_websocket import WebsocketServer
async def server():
websocket_server = WebsocketServer()
async with serve(websocket_server.serve, "localhost", 1234):
await asyncio.Future() # run forever
asyncio.run(server())
Or with an ASGI server:
# main.py
import uvicorn
from ypy_websocket.asgi import Server
app = Server()
if __name__ == "__main__":
uvicorn.run("main:app", port=5000, log_level="info")
WebSocket API
The WebSocket object passed to WebsocketProvider
and WebsocketServer.serve
must respect the
following API:
class WebSocket:
@property
def path(self) -> str:
# can be e.g. the URL path
# or a room identifier
return "my-roomname"
def __aiter__(self):
return self
async def __anext__(self) -> bytes:
# async iterator for receiving messages
# until the connection is closed
try:
message = await self.recv()
except:
raise StopAsyncIteration()
return message
async def send(self, message: bytes):
# send message
pass
async def recv(self) -> bytes:
# receive message
return b""
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
ypy_websocket-0.10.0.tar.gz
(12.4 kB
view hashes)
Built Distribution
Close
Hashes for ypy_websocket-0.10.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6696623dbef0e99b38d14d7fe58ec32ba13f8f462170553254e7b176ced0e749 |
|
MD5 | c240eaee823492f6282fe1e7d61af8aa |
|
BLAKE2b-256 | 39b178a73d79d7129e85c58ee7900686f9b96e2ba90cfbb55cf4c6cbf3ea0156 |