Kitware/wslink

Name: wslink

Owner: Kitware, Inc.

Description: Python/JavaScript library for communicating over WebSocket

Created: 2017-05-09 20:08:23.0

Updated: 2018-03-18 00:30:49.0

Pushed: 2018-03-01 19:43:29.0

Homepage: https://kitware.github.io/wslink/

Size: 343

Language: Python

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

wslink

Build Status

Wslink allows easy, bi-directional communication between a python server and a javascript client over a websocket. The client can make remote procedure calls (RPC) to the server, and the server can publish messages to topics that the client can subscribe to. The server can include binary attachments in these messages, which are communicated as a binary websocket message, avoiding the overhead of encoding and decoding.

RPC and publish/subscribe

The initial users of wslink driving its development are VTK and ParaViewWeb. ParaViewWeb and vtkWeb require:

Wslink is replacing a communication layer based on Autobahn WAMP, and so one of the goals is to be fairly compatible with WAMP, but simplify the interface to the point-to-point communication we actually use.

Examples
Testing
Existing API

Existing ParaViewWeb applications use these code patterns:

We don't support introspection or initial handshake about which methods are supported - the client and server must be in sync.

Message format:


t request = {
wslink: 1.0,
id: `rpc:${clientId}:${count}`,
method: 'myapp.render.window.image',
args: [],
kwargs: { w: 512, h: 512 }


t response = {
wslink: 1.0,
id: `rpc:${clientId}:${count}`,
result: {}, // either result or error, not both
error: {}


ypes used as prefix for id.
t types = ['rpc', 'publish', 'system'];

ython
dd a binary attachment
getImage(self):
return {
    "size": [512, 512],
    "blob": session.addAttachment(memoryview(dataArray)),
    "mtime": dataArray.getMTime()
}
Binary attachments

session.addAttachment() takes binary data and stores it, returning a string key that will be associated with the attachment. When a message is sent that uses the attachment key, a text header message and a binary message is sent beforehand with each attachment. The client will then substitute the binary buffer for the string key when it receives the final message.

Subscribe

The client tracks subscriptions - the server currently blindly sends out messages for any data it produces which might be subscribed to. This is not very efficient - if the client notifies the server of a subscription, it can send the data only when someone is listening. The ParaViewWeb app Visualizer makes an RPC call after subscribing to tell the server to start publishing.

Handshake

When the client initially connects, it sends a 'hello' to authenticate with the server, so the server knows this client can handle the messages it sends, and the server can provide the client with a unique client ID - which the client must embed in the rpc “id” field of its messages to the server.

Design

More extensive discussion in the design document.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.