Skip to main content

WSRPC WebSocket RPC for tornado

Project description

https://travis-ci.org/mosquito/wsrpc.svg

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.

Installation

Install via pip:

pip install wsrpc-tornado

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>

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-tornado-0.5.2.tar.gz (30.9 kB view details)

Uploaded Source

Built Distribution

wsrpc_tornado-0.5.2-py2-none-any.whl (34.3 kB view details)

Uploaded Python 2

File details

Details for the file wsrpc-tornado-0.5.2.tar.gz.

File metadata

File hashes

Hashes for wsrpc-tornado-0.5.2.tar.gz
Algorithm Hash digest
SHA256 9c1a9bf40e09a2655adc600bf21e7f2a2463f48bfb597788b1130e0635897121
MD5 e3a6ce398f96412e821a575523e9473f
BLAKE2b-256 1d8f0f71b075b207848c1a466f14d5a2e046da3d9ee2400569f3ad52bee513c0

See more details on using hashes here.

File details

Details for the file wsrpc_tornado-0.5.2-py2-none-any.whl.

File metadata

File hashes

Hashes for wsrpc_tornado-0.5.2-py2-none-any.whl
Algorithm Hash digest
SHA256 9e659b24e67321a3747d345bbdd205e81321c3895ee24222a3e6a20edaad7f65
MD5 029a595d1fb5f3e029e0d18ecc3b66bc
BLAKE2b-256 5302960845db6d14967d7e2a9222862f7712a218339af21ecb9ffa5ac2bd8106

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