Skip to main content

WSRPC is the RPC over WebSocket for aiohttp

Project description

Travis CI Latest Version python wheel Python Versions license

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

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

<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:

def do_notify(self):
    awesome = 'Notification for you!'
    yield self.socket.call('notify', result=awesome)

frontend:

<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.

Example

Example running there demo.

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

wsrpc-aiohttp-2.2.0.tar.gz (16.2 kB view details)

Uploaded Source

Built Distribution

wsrpc_aiohttp-2.2.0-py3-none-any.whl (60.6 kB view details)

Uploaded Python 3

File details

Details for the file wsrpc-aiohttp-2.2.0.tar.gz.

File metadata

  • Download URL: wsrpc-aiohttp-2.2.0.tar.gz
  • Upload date:
  • Size: 16.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for wsrpc-aiohttp-2.2.0.tar.gz
Algorithm Hash digest
SHA256 91d764904470fc43e614581d835c244f2db6e6ba01fe4b2b898ab3608f7a1325
MD5 c5b015ff36c5c30f78513270b10e21ae
BLAKE2b-256 9dd9f72b1c76d7835e79c810b5972de2035e02f639e072de0b86186c91a7595f

See more details on using hashes here.

File details

Details for the file wsrpc_aiohttp-2.2.0-py3-none-any.whl.

File metadata

  • Download URL: wsrpc_aiohttp-2.2.0-py3-none-any.whl
  • Upload date:
  • Size: 60.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for wsrpc_aiohttp-2.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5e9d907d8a2ca260d3756cf570e16efb424fb067aae20b754777000e8e95e211
MD5 6b51483ba3e6a2c537a4566bd96982c9
BLAKE2b-256 45913dce1ffab8825e4a184b4ce47fbbda6d83139e42fe697d1868bdab3957f4

See more details on using hashes here.

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