0.8.3 • Published 6 years ago

nisper v0.8.3

Weekly downloads
4
License
ISC
Repository
-
Last release
6 years ago

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);
});
0.8.3

6 years ago

0.8.2

6 years ago

0.8.1

7 years ago

0.8.0

7 years ago

0.7.0

7 years ago

0.6.27

7 years ago

0.6.26

7 years ago

0.6.25

7 years ago

0.6.24

7 years ago

0.6.23

7 years ago

0.6.22

7 years ago

0.6.21

7 years ago

0.6.20

7 years ago

0.6.19

7 years ago

0.6.18

7 years ago

0.6.17

7 years ago

0.6.16

7 years ago

0.6.15

7 years ago

0.6.14

7 years ago

0.6.13

7 years ago

0.6.12

7 years ago

0.6.11

7 years ago

0.6.10

7 years ago

0.6.9

7 years ago

0.6.8

7 years ago

0.6.7

7 years ago

0.6.6

7 years ago

0.6.5

7 years ago

0.6.4

7 years ago

0.6.3

7 years ago

0.6.2

7 years ago

0.6.1

7 years ago

0.6.0

7 years ago

0.5.4

7 years ago

0.5.3

7 years ago

0.5.2

7 years ago

0.4.2

7 years ago

0.5.1

7 years ago

0.5.0

7 years ago

0.4.1

7 years ago

0.4.0

7 years ago

0.3.3

7 years ago

0.3.2

7 years ago

0.3.1

7 years ago

0.3.0

7 years ago

0.2.17

8 years ago

0.2.16

8 years ago

0.2.15

8 years ago

0.2.14

8 years ago

0.2.13

8 years ago

0.2.12

8 years ago

0.2.11

8 years ago

0.2.10

8 years ago

0.2.9

8 years ago

0.2.8

8 years ago

0.2.7

8 years ago

0.2.6

8 years ago

0.2.5

8 years ago

0.2.4

8 years ago

0.2.3

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.0

8 years ago

0.0.9

8 years ago

0.0.8

8 years ago

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago