WSRPC is the RPC over WebSocket for aiohttp
Project description
WSRPC aiohttp
=============
.. image:: https://travis-ci.org/wsrpc/wsrpc-aiohttp.svg
:target: https://travis-ci.org/wsrpc/wsrpc-aiohttp
:alt: Travis CI
.. image:: https://img.shields.io/pypi/v/wsrpc-aiohttp.svg
:target: https://pypi-hypernode.com/pypi/wsrpc-aiohttp/
:alt: Latest Version
.. image:: https://img.shields.io/pypi/wheel/wsrpc-aiohttp.svg
:target: https://pypi-hypernode.com/pypi/wsrpc-aiohttp/
.. image:: https://img.shields.io/pypi/pyversions/wsrpc-aiohttp.svg
:target: https://pypi-hypernode.com/pypi/wsrpc-aiohttp/
.. image:: https://img.shields.io/pypi/l/wsrpc-aiohttp.svg
:target: https://pypi-hypernode.com/pypi/wsrpc-aiohttp/
Remote Procedure call through WebSocket between browser and tornado.
Features
++++++++
* Initiating call client function from server side.
* Calling the server method from the client.
* Transferring any exceptions from a client side to the server side and vise versa.
* The frontend-library are well done for usage without any modification.
* Fully asynchronous server-side functions.
* Thread-based websocket handler for writing fully-synchronous code (for synchronous database drivers etc.)
* Protected server-side methods (starts with underline never will be call from clients-side directly)
* Asynchronous connection protocol. Server or client can call multiple methods with unpredictable ordering of answers.
* If `ujson`_ is installed messages will be serialize/deserialize with it.
Installation
------------
Install via pip::
pip install wsrpc-aiohttp
Install ujson if you want::
pip install ujson
Simple usage
------------
Add the backend side
.. code-block:: python
from time import time
## If you want write async tornado code import it
# from from wsrpc import WebSocketRoute, WebSocket, wsrpc_static
## else you should use thread-base handler
from wsrpc import WebSocketRoute, WebSocketThreaded as WebSocket, wsrpc_static
tornado.web.Application((
# js static files will available as "/js/wsrpc.min.js".
wsrpc_static(r'/js/(.*)'),
# WebSocket handler. Client will connect here.
(r"/ws/", WebSocket),
# Serve other static files
(r'/(.*)', tornado.web.StaticFileHandler, {
'path': os.path.join(project_root, 'static'),
'default_filename': 'index.html'
}),
))
# This class should be call by client.
# Connection object will be have the instance of this class when will call route-alias.
class TestRoute(WebSocketRoute):
# This method will be executed when client will call route-alias first time.
def init(self, **kwargs):
# the python __init__ must be return "self". This method might return anything.
return kwargs
def getEpoch(self):
# this method named by camelCase because the client can call it.
return time()
# stateful request
# this is the route alias TestRoute as "test1"
WebSocket.ROUTES['test1'] = TestRoute
# stateless request
WebSocket.ROUTES['test2'] = lambda *a, **kw: True
# initialize ThreadPool. Needed when using WebSocketThreaded.
WebSocket.init_pool()
Add the frontend side
.. code-block:: HTML
<script type="text/javascript" src="/js/q.min.js"></script>
<script type="text/javascript" src="/js/wsrpc.min.js"></script>
<script>
var url = window.location.protocol==="https:"?"wss://":"ws://" + window.location.host + '/ws/';
RPC = WSRPC(url, 5000);
RPC.addRoute('test', function (data) { return "Test called"; });
RPC.connect();
RPC.call('test1.getEpoch').then(function (data) {
console.log(data);
}, function (error) {
alert(error);
}).done();
RPC.call('test2').then(function (data) { console.log(data); }).done();
</script>
Reverse call from Server to Client
----------------------------------
backend:
.. code-block:: python
def do_notify(self):
awesome = 'Notification for you!'
yield self.socket.call('notify', result=awesome)
frontend:
.. code-block:: HTML
<script>
var url = (window.location.protocol==="https:"?"wss://":"ws://") + window.location.host + '/ws/';
RPC = WSRPC(url, 5000);
RPC.addRoute('notify', function (data) { return data.result; });
RPC.connect();
</script>
Documentation
+++++++++++++
All available `documentation here`_.
.. _documentation here: https://docs.wsrpc.info/
Example
+++++++
Example running there demo_.
.. _demo: https://demo.wsrpc.info/
=============
.. image:: https://travis-ci.org/wsrpc/wsrpc-aiohttp.svg
:target: https://travis-ci.org/wsrpc/wsrpc-aiohttp
:alt: Travis CI
.. image:: https://img.shields.io/pypi/v/wsrpc-aiohttp.svg
:target: https://pypi-hypernode.com/pypi/wsrpc-aiohttp/
:alt: Latest Version
.. image:: https://img.shields.io/pypi/wheel/wsrpc-aiohttp.svg
:target: https://pypi-hypernode.com/pypi/wsrpc-aiohttp/
.. image:: https://img.shields.io/pypi/pyversions/wsrpc-aiohttp.svg
:target: https://pypi-hypernode.com/pypi/wsrpc-aiohttp/
.. image:: https://img.shields.io/pypi/l/wsrpc-aiohttp.svg
:target: https://pypi-hypernode.com/pypi/wsrpc-aiohttp/
Remote Procedure call through WebSocket between browser and tornado.
Features
++++++++
* Initiating call client function from server side.
* Calling the server method from the client.
* Transferring any exceptions from a client side to the server side and vise versa.
* The frontend-library are well done for usage without any modification.
* Fully asynchronous server-side functions.
* Thread-based websocket handler for writing fully-synchronous code (for synchronous database drivers etc.)
* Protected server-side methods (starts with underline never will be call from clients-side directly)
* Asynchronous connection protocol. Server or client can call multiple methods with unpredictable ordering of answers.
* If `ujson`_ is installed messages will be serialize/deserialize with it.
Installation
------------
Install via pip::
pip install wsrpc-aiohttp
Install ujson if you want::
pip install ujson
Simple usage
------------
Add the backend side
.. code-block:: python
from time import time
## If you want write async tornado code import it
# from from wsrpc import WebSocketRoute, WebSocket, wsrpc_static
## else you should use thread-base handler
from wsrpc import WebSocketRoute, WebSocketThreaded as WebSocket, wsrpc_static
tornado.web.Application((
# js static files will available as "/js/wsrpc.min.js".
wsrpc_static(r'/js/(.*)'),
# WebSocket handler. Client will connect here.
(r"/ws/", WebSocket),
# Serve other static files
(r'/(.*)', tornado.web.StaticFileHandler, {
'path': os.path.join(project_root, 'static'),
'default_filename': 'index.html'
}),
))
# This class should be call by client.
# Connection object will be have the instance of this class when will call route-alias.
class TestRoute(WebSocketRoute):
# This method will be executed when client will call route-alias first time.
def init(self, **kwargs):
# the python __init__ must be return "self". This method might return anything.
return kwargs
def getEpoch(self):
# this method named by camelCase because the client can call it.
return time()
# stateful request
# this is the route alias TestRoute as "test1"
WebSocket.ROUTES['test1'] = TestRoute
# stateless request
WebSocket.ROUTES['test2'] = lambda *a, **kw: True
# initialize ThreadPool. Needed when using WebSocketThreaded.
WebSocket.init_pool()
Add the frontend side
.. code-block:: HTML
<script type="text/javascript" src="/js/q.min.js"></script>
<script type="text/javascript" src="/js/wsrpc.min.js"></script>
<script>
var url = window.location.protocol==="https:"?"wss://":"ws://" + window.location.host + '/ws/';
RPC = WSRPC(url, 5000);
RPC.addRoute('test', function (data) { return "Test called"; });
RPC.connect();
RPC.call('test1.getEpoch').then(function (data) {
console.log(data);
}, function (error) {
alert(error);
}).done();
RPC.call('test2').then(function (data) { console.log(data); }).done();
</script>
Reverse call from Server to Client
----------------------------------
backend:
.. code-block:: python
def do_notify(self):
awesome = 'Notification for you!'
yield self.socket.call('notify', result=awesome)
frontend:
.. code-block:: HTML
<script>
var url = (window.location.protocol==="https:"?"wss://":"ws://") + window.location.host + '/ws/';
RPC = WSRPC(url, 5000);
RPC.addRoute('notify', function (data) { return data.result; });
RPC.connect();
</script>
Documentation
+++++++++++++
All available `documentation here`_.
.. _documentation here: https://docs.wsrpc.info/
Example
+++++++
Example running there demo_.
.. _demo: https://demo.wsrpc.info/
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
wsrpc-aiohttp-0.2.5.tar.gz
(52.4 kB
view details)
Built Distribution
File details
Details for the file wsrpc-aiohttp-0.2.5.tar.gz
.
File metadata
- Download URL: wsrpc-aiohttp-0.2.5.tar.gz
- Upload date:
- Size: 52.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 805a99bee021726bb1248e42d29391fb264e04fd56e18d33ab42c3acb0304158 |
|
MD5 | 29f2c1edf731bd7eb740d2e8acb096ba |
|
BLAKE2b-256 | d6aedff5eb535796fef9765d6dac71799b9542dfe87e63c904cc77b5eaaf2cb5 |
File details
Details for the file wsrpc_aiohttp-0.2.5-py3-none-any.whl
.
File metadata
- Download URL: wsrpc_aiohttp-0.2.5-py3-none-any.whl
- Upload date:
- Size: 57.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fbbad682abd07abdbd73868c08d7188d288c201b8f0bbf507ebf6de364d4d743 |
|
MD5 | 7b8a135bd1eb29474d6439077aede0e7 |
|
BLAKE2b-256 | b7c2c832255a7070e1744381356ad0f130d5c22b5e90b6ff9b5d5db41024d995 |