WebSocket connector for Ypy
Project description
ypy-websocket
ypy-websocket is an async WebSocket connector for Ypy.
Usage
Client
Here is a code example:
import asyncio
from websockets import connect
from ypy_websocket import YDoc, WebsocketProvider
async def client():
ydoc = YDoc()
websocket = await connect("ws://localhost:1234/my-roomname")
WebsocketProvider(ydoc, websocket)
ymap = ydoc.get_map("map")
with ydoc.begin_transaction() as t:
ymap.set(t, "key", "value")
asyncio.run(client())
Note that YDoc
has to be imported from ypy_websocket
instead of y_py
. This will change in the
future, when y_py
has the necessary event handlers. ypy_websocket.YDoc
is a subclass of
y_py.YDoc
.
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())
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.1.13.tar.gz
(7.3 kB
view hashes)
Built Distribution
Close
Hashes for ypy_websocket-0.1.13-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 37e77e1cdc7073d512417bc59264549e15bde06539cc8bdfb01b3aa36b867017 |
|
MD5 | 6fb6b5440dd16bf5a55ab96c66011df3 |
|
BLAKE2b-256 | d77f110931b922e52d382b362c0fa3436c5a9e135fb72f315ca539904012de0a |