# nisper

> A RPC lib based on websocket protocol and nisp language

Latest version **0.8.3** (published 2018-02-05) · ISC license · 0 weekly downloads

## Install

```sh
npm install nisper
pnpm add nisper
yarn add nisper
bun add nisper
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.8.3 |
| Published | 2018-02-05 |
| First published | 2016-04-14 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| Maintainers | ysmood |

## Links

- npm: https://www.npmjs.com/package/nisper
- npm.io page: https://npm.io/package/nisper

## Dependencies (5)

- [ws](https://npm.io/package/ws.md) 3.3.3
- [nisp](https://npm.io/package/nisp.md) 0.13.4
- [yaku](https://npm.io/package/yaku.md) 0.18.5
- [commander](https://npm.io/package/commander.md) ^2.13.0
- [@types/node](https://npm.io/package/@types/node.md) 8.5.1

## Recent versions

- 0.8.3 (latest) — 2018-02-05
- 0.8.2 — 2017-12-17
- 0.8.1 — 2017-05-25
- 0.8.0 — 2017-05-25
- 0.7.0 — 2017-05-12
- 0.6.27 — 2017-03-31
- 0.6.26 — 2017-03-31
- 0.6.25 — 2017-03-31
- 0.6.24 — 2017-03-20
- 0.6.23 — 2017-03-12
- 0.6.22 — 2017-03-10
- 0.6.21 — 2017-03-10
- 0.6.20 — 2017-03-10
- 0.6.19 — 2017-03-07
- 0.6.18 — 2017-03-07
- … 57 more at https://npm.io/package/nisper/versions

## README

# Overview

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

[![NPM version](https://badge.fury.io/js/nisper.svg)](http://badge.fury.io/js/nisper) [![Build Status](https://travis-ci.org/ysmood/nisper.svg)](https://travis-ci.org/ysmood/nisper) [![Deps Up to Date](https://david-dm.org/ysmood/nisper.svg?style=flat)](https://david-dm.org/ysmood/nisper)


# 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:

```js
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:

```js
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


```js
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:

```js
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

```js
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

```js
var call = require('nisper/lib/call');


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

[nisp]: https://github.com/ysmood/nisp

---
_Source: https://npm.io/package/nisper · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
