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

Uploaded Source

Built Distribution

ypy_websocket-0.7.1-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ypy_websocket-0.7.1.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.23.0

File hashes

Hashes for ypy_websocket-0.7.1.tar.gz
Algorithm Hash digest
SHA256 aa1366772e3dca1a657142874318d4d46a39a120783d743884562ca5ab582f6c
MD5 9d58c569a92a4cc795a88c51619a1aac
BLAKE2b-256 d3c3784d40e883f61362f6fb9a1b2d53ad390e607066947c4669c7609f93d6bd

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for ypy_websocket-0.7.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ee39245f0f35963d9fdafb5717d2dc1020ac3f04085e359fb0f0cce3b5836ea7
MD5 acdb21b9da9708a0f51659b99edd26f9
BLAKE2b-256 c146fd906ac3d38b208f172da6030b4a3aebb32a3a172c10d450e9259f4b3053

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