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.9.tar.gz
(6.8 kB
view details)
Built Distribution
File details
Details for the file ypy_websocket-0.1.9.tar.gz
.
File metadata
- Download URL: ypy_websocket-0.1.9.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.10.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ab89bc1917f7e65a51d97127cd6f3a109292751e6cd32f48926a71d83f2ce6aa |
|
MD5 | 56881b2f3d824078407f144b4f9c4379 |
|
BLAKE2b-256 | 10077c3895cd2870597ca91b34be1ce429c6c2e0feb7f2f5d09bd5ccb13bcf4f |
Provenance
File details
Details for the file ypy_websocket-0.1.9-py2.py3-none-any.whl
.
File metadata
- Download URL: ypy_websocket-0.1.9-py2.py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.10.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | efd0207df6bca064716a8c2dded8e918f19c5c8ba302de4305eebab35bb9e511 |
|
MD5 | 105684ae5b67e4372fb3807164f50b40 |
|
BLAKE2b-256 | 2e1ac89f1b8309bb3f5cf77ec416dbba1916dc40dc0e918bc7934d3bda954412 |