Skip to main content

WSRPC WebSocket RPC for tornado

Project description

Remote Procedure call through WebSocket between browser and tornado.

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
from wsrpc import WebSocketRoute, WebSocketThreaded as WebSocket, wsrpc_static

tornado.web.Application((
    wsrpc_static(r'/js/(.*)'),
    (r"/ws/", WebSocket),
    (r'/(.*)', tornado.web.StaticFileHandler, {
         'path': os.path.join(project_root, 'static'),
         'default_filename': 'index.html'
    }),
))

class TestRoute(WebSocketRoute):
    def init(self, **kwargs):
        return kwargs

    def getEpoch(self):
        return time()

# stateful request
WebSocket.ROUTES['test1'] = TestRoute

# stateless request
WebSocket.ROUTES['test2'] = lambda *a, **kw: True

# initialize ThreadPool
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.1.tar.gz (29.6 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: wsrpc-tornado-0.5.1.tar.gz
  • Upload date:
  • Size: 29.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for wsrpc-tornado-0.5.1.tar.gz
Algorithm Hash digest
SHA256 9294c05cf7a7dc8c11b73afab1977efc8b5094bb7095f6b26447f2a8933ede82
MD5 2a5d50dc61c46675f6df9bd016b6fdf5
BLAKE2b-256 2d2715fe46c9283ff49d678677e17b000482e3e6dea12bfce86f497c31c9a5a7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page