nisper

0.8.3 • Public • Published

Overview

Nisper is a RPC lib based on websocket protocol and nisp language.

NPM version Build Status Deps Up to Date

Features

  • Script based call makes the functions composable, you can even write a complex program to remote
  • Safe by design, full control of the user's authority
  • Same api for both node to browser, browser to node and node to node
  • Full support for Promise calls
  • Supports binary data type (json by default)
  • Bidirectional communication
  • Auto reconnect

Example

For more usage, read the unit test test/index.js.

Live Demo: https://runkit.com/ysmood/nisper2

Echo Example

Node Server:

import nisper from 'nisper';
import $ from 'nisp/lib/$';
 
const server = nisper({
    wsOptions: { port: 8080 },
    sandbox: {
        $,
        // Define a function, client can call it remotely.
        '+': (a, b) => a + b
    },
    onOpen: (ws) => {
        // When a client connected, call it
        server.call(ws, ['-', 2, 1]).then(res => {
            console.log('client res:', res); // => 1
        });
    }
});
 

Browser or node client:

import nisper from 'nisper';
 
const client = nisper({
    url: `ws://127.0.0.1:8080`,
    sandbox: {
        // Define a function, server can call it remotely.
        '-': (a, b) => a - b
    }
});
 
// add(1, add(1, 1))
client.callx`(+ 1 (+ 1 1))`.then(res => {
    console.log('server res:', res); // => 3
});
 

Composable async function & msgpack

import nisper from 'nisper';
import async from 'nisp/lib/async';
import msgpack from 'msgpack-lite';
 
const server = nisper({
    wsOptions: { port: 8080 },
    encode: msgpack.encode,
    decode: msgpack.decode,
    sandbox: {
        // Define a function, client can call it remotely.
        // This add function will return the sum after 1 second.
        '+': async((a, b) =>
            new Promise(resolve =>
                setTimeout(resolve, 1000, a + b)
            )
        )
    }
});

Browser or node client:

import nisper from 'nisper';
import msgpack from 'msgpack-lite';
 
const client = nisper({
    url: `ws://127.0.0.1:8080`,
    encode: msgpack.encode,
    decode: msgpack.decode
});
 
// add(1, add(1, 1))
client.call(['+', 1, ['+', 1, 1]]).then(res => {
    // It will log 3 after 2 second.
    console.log('server res:', res);
});

API

nisper = ({
    // node native http.Server
    httpServer: null,
 
    // string, such as `ws://a.com`
    url: null,
 
    sandbox: {},
 
    onOpen: (connection) => env,
 
    onRequest: () => env,
 
    error: (err) => Error,
 
    isAutoReconnect: true,
    retrySpan: 1000,
    timeout: 1000 * 60 * 2,
 
    encode: (Object) => String || Buffer,
    decode: (String) => Object,
 
    // Same options as ws: https://github.com/websockets/ws/blob/master/doc/ws.md
    wsOptions: Object,
 
    isDebug: false
}) => {
    sandbox: Object,
    close: Function,
    websocketClient: Object,
    websocketServer: Object,
    middleware: Function,
    call: Function
};

call only once

var call = require('nisper/lib/call');
 
 
call('ws://127.0.0.1:8080', ['echo', 'hi']).then(res => {
    console.log(res);
});

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.8.35latest

Version History

VersionDownloads (Last 7 Days)Published
0.8.35
0.8.20
0.8.11
0.8.01
0.7.00
0.6.270
0.6.260
0.6.250
0.6.241
0.6.230
0.6.220
0.6.211
0.6.200
0.6.190
0.6.180
0.6.170
0.6.160
0.6.150
0.6.140
0.6.131
0.6.120
0.6.110
0.6.100
0.6.90
0.6.80
0.6.70
0.6.60
0.6.510
0.6.40
0.6.30
0.6.20
0.6.10
0.6.00
0.5.40
0.5.30
0.5.20
0.4.20
0.5.10
0.5.00
0.4.10
0.4.01
0.3.30
0.3.20
0.3.10
0.3.04
0.2.172
0.2.160
0.2.156
0.2.140
0.2.130
0.2.120
0.2.110
0.2.100
0.2.90
0.2.80
0.2.71
0.2.60
0.2.50
0.2.40
0.2.30
0.2.10
0.2.00
0.1.00
0.0.90
0.0.80
0.0.71
0.0.61
0.0.50
0.0.40
0.0.30
0.0.20
0.0.10

Package Sidebar

Install

npm i nisper

Weekly Downloads

20

Version

0.8.3

License

ISC

Last publish

Collaborators

  • ysmood