Skip to main content

WebSocket connector for Ypy

Project description

Build Status Code style: black

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


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.12.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

ypy_websocket-0.1.12-py2.py3-none-any.whl (7.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file ypy_websocket-0.1.12.tar.gz.

File metadata

  • Download URL: ypy_websocket-0.1.12.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.10.4

File hashes

Hashes for ypy_websocket-0.1.12.tar.gz
Algorithm Hash digest
SHA256 f11b8b5adb37957bb3536704710882442aca798fad8fbe3182069d7e7f560f54
MD5 96cb2ae71a6b2482b9f4c270f334b548
BLAKE2b-256 8be24291ce9ee699f1b2580653a5e4591713ab3c88bbbcca52e5849a27f73c58

See more details on using hashes here.

Provenance

File details

Details for the file ypy_websocket-0.1.12-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for ypy_websocket-0.1.12-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 0f4a5879e8c87ca96cfcfa3938e467ce0f94bf4bcee6a7eda12b330292252846
MD5 17c4962e64729e3ced2e6aa4bad144fe
BLAKE2b-256 6eecebde1fa63d2c27b4cd5c35728489a77e6b8d00ffe366489f97f35279048b

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page