Git Product home page Git Product logo

WSRPC Tornado

Travis CI Latest Version

Easy to use minimal WebSocket Remote Procedure Call library for tornado servers. See online demo.

Also, there is aiohttp WSRPC implementation.

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>

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>

WSRPC's Projects

WSRPC doesnโ€™t have any public repositories yet.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.