0.8.3 • Published 8 years ago

nisper v0.8.3

Weekly downloads
4
License
ISC
Repository
-
Last release
8 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

8 years ago

0.8.2

8 years ago

0.8.1

9 years ago

0.8.0

9 years ago

0.7.0

9 years ago

0.6.27

9 years ago

0.6.26

9 years ago

0.6.25

9 years ago

0.6.24

9 years ago

0.6.23

9 years ago

0.6.22

9 years ago

0.6.21

9 years ago

0.6.20

9 years ago

0.6.19

9 years ago

0.6.18

9 years ago

0.6.17

9 years ago

0.6.16

9 years ago

0.6.15

9 years ago

0.6.14

9 years ago

0.6.13

9 years ago

0.6.12

9 years ago

0.6.11

9 years ago

0.6.10

9 years ago

0.6.9

9 years ago

0.6.8

9 years ago

0.6.7

9 years ago

0.6.6

9 years ago

0.6.5

9 years ago

0.6.4

9 years ago

0.6.3

9 years ago

0.6.2

9 years ago

0.6.1

9 years ago

0.6.0

9 years ago

0.5.4

9 years ago

0.5.3

9 years ago

0.5.2

9 years ago

0.4.2

9 years ago

0.5.1

9 years ago

0.5.0

9 years ago

0.4.1

9 years ago

0.4.0

9 years ago

0.3.3

9 years ago

0.3.2

9 years ago

0.3.1

9 years ago

0.3.0

9 years ago

0.2.17

9 years ago

0.2.16

9 years ago

0.2.15

10 years ago

0.2.14

10 years ago

0.2.13

10 years ago

0.2.12

10 years ago

0.2.11

10 years ago

0.2.10

10 years ago

0.2.9

10 years ago

0.2.8

10 years ago

0.2.7

10 years ago

0.2.6

10 years ago

0.2.5

10 years ago

0.2.4

10 years ago

0.2.3

10 years ago

0.2.1

10 years ago

0.2.0

10 years ago

0.1.0

10 years ago

0.0.9

10 years ago

0.0.8

10 years ago

0.0.7

10 years ago

0.0.6

10 years ago

0.0.5

10 years ago

0.0.4

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago