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
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())

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

Uploaded Source

Built Distribution

ypy_websocket-0.8.2-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ypy_websocket-0.8.2.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.8

File hashes

Hashes for ypy_websocket-0.8.2.tar.gz
Algorithm Hash digest
SHA256 491b2cc4271df4dde9be83017c15f4532b597dc43148472eb20c5aeb838a5b46
MD5 ff7afbc5d48799bbe1e79cd40c6b67b0
BLAKE2b-256 5b128937dcca49af5ec50346f28ebc90112c458595f75e46f31e3fdcca29bda8

See more details on using hashes here.

Provenance

File details

Details for the file ypy_websocket-0.8.2-py3-none-any.whl.

File metadata

File hashes

Hashes for ypy_websocket-0.8.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9049d5a7d61c26c2b5a39757c9ffcbe2274bf3553adeea8de7fe1c04671d4145
MD5 4decf9572968ad8c0ebdc5a58bf16a7c
BLAKE2b-256 71026b4f3a8df6713654caf6c84ca8a21ccdd546d8fa58a887547afbcf453c68

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