Metadata-Version: 2.1
Name: rgws
Version: 0.1.4
Summary: Websockets extended with RPC.
Home-page: https://github.com/Redict/rg_websocket
Author: canvas123
Author-email: nurik040404@gmail.com
License: MIT
Platform: UNKNOWN
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Environment :: Web Environment
Requires-Python: >=3.6.0
Description-Content-Type: text/markdown
Requires-Dist: websockets


# RG Websocket exended with RPC

Simple interface for writing Websocket application with RPC.

## Installation

### PIP official

`pip install rgws`

### Dev 
`pip install git+https://github.com/Redict/rg_websocket.git`

## Usage:

### Server

```python
class SimpleServerInterface(WebsocketServer):
    def __init__(self, **kwargs):
        super(SimpleServerInterface, self).__init__(**kwargs)
        self._register(self.example_func)

    """
    This overrides _consumer method in WebsocketServer, there should
    business logic be placed if any. At this point we are just 
    dispatching function from message and sending result back.
    """
    async def _consumer(self, websocket, message):
        ret = await self.dispatch(message)
        async for gen in ret:
            await websocket.send(gen)

    async def example_func(self, bla):
        yield json.dumps({"resp": bla})

    async def stream_func(self):
        data = [0] * 2 ** 32
        return self.make_data_stream(data)
```

### Client

```python
class SimpleClientInterface(WebsocketClient):
    def __init__(self, **kwargs):
        super(SimpleClientInterface, self).__init__(**kwargs)

    """
    This is business logic for client, basically in this example
    we just connects to server and trying to call `example_func` once
    then exits.
    """

    async def _producer(self, websocket):
        logging.debug(await self.example_func("blo"))
        # if you want to pass function with arguments, you can use functools.partial(func, args)
        logging.debug(await self.read_data_stream(websocket, self.stream_func))
```

You can make request with other way:
```python
c = SimpleClientInterface(host=..., port=...)

async def main():
    while True:
        if not c:
            continue
        logging.debug(c.example_func())

if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(asyncio.gather(c.run(), main()))
```

