# awaited.io

> A minimalistic socket.io RPC using async/await

Latest version **0.0.8** (published 2020-02-22) · BSD-3-Clause license · 0 weekly downloads

## Install

```sh
npm install awaited.io
pnpm add awaited.io
yarn add awaited.io
bun add awaited.io
```

## 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.0.8 |
| Published | 2020-02-22 |
| First published | 2017-04-03 |
| Weekly downloads | 0 |
| License | BSD-3-Clause |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 16.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Albert Calbet |
| Maintainers | kalvin |

## Links

- npm: https://www.npmjs.com/package/awaited.io
- Repository: https://github.com/kalvinarts/awaited.io
- Issues: https://github.com/kalvinarts/awaited.io/issues
- npm.io page: https://npm.io/package/awaited.io

## Dependencies (2)

- [socket.io](https://npm.io/package/socket.io.md) ^2.3.0
- [socket.io-client](https://npm.io/package/socket.io-client.md) ^2.3.0

## Recent versions

- 0.0.8 (latest) — 2020-02-22
- 0.0.7 — 2017-10-20
- 0.0.6 — 2017-10-20
- 0.0.5 — 2017-04-03
- 0.0.4 — 2017-04-03
- 0.0.3 — 2017-04-03
- 0.0.2 — 2017-04-03
- 0.0.1 — 2017-04-03

## README

# awaited.io

## A minimalistic [socket.io](https://socket.io/) RPC using Promises and async/await

### Index

* [Install](#install)
* [Usage - server](#usage---server)
* [Usage - client](#usage---client)
* [Registering functions](#registering-functions)
* [Middleware](#middleware)
* [Calling functions from the client](#calling-functions-from-the-client)
* [Errors](#errors)
* [The client is a server and the server is a client](#the-client-is-a-server-and-the-server-is-a-client)
* [Examples](#Examples)
* [Coffee contributions accepted](#Coffee-contributions-accepted)
* [License](#license)

### Install

`npm install awaited.io`

### Usage - server

Let's make a simple server that exposes an API to operate the an ES6 Map object.

The Map will be exposed to our API functions via a shared context so all the clients connecting with our RPC server will access the same Map instance.

```javascript
const AwaitedIO = require('awaited.io');
const SocketIO  = require('socket.io');

const api   = {}
api.mapHas  = (ctx, key) => ctx.map.has(key);
api.mapGet  = (ctx, key) => ctx.map.get(key);
api.mapSet  = (ctx, key, value) => ctx.map.set(key, value);

const ctx   = {
  map: new Map()
};

const io = new SocketIO().listen(3131);

io.on('connection', socket => {
  const aio = new AwaitedIO(socket, { ctx });

  aio.registerAPI(api);
});
```

### Usage - client

Now let's make a client to connect to our remote API

```javascript
const AwaitedIO = require('awaited.io');
const ClientIO  = require('socket.io-client');

async function main () {
  const socket    = new ClientIO('http://localhost:3131');
  const aioClient = new AwaitedIO(socket);

  const remote = await aioClient.update();

  await remote.mapSet('foo', 'bar');
  console.log('-- response:', await remote.mapHas('foo'));
  // -- response: true
  console.log('-- response:', await remote.mapGet('foo'));
  // -- response: bar
}

main();
```

That's it ;)

### Registering functions

To register functions to be exposed to the client side you can use the `register` and the `registerAPI` methods.

Registered functions that return a Promise will be awaited until the promise is resolved or rejected to send its response to the client side.

__register__ : _function (name, func)_ - Registers a function to be exposed.

__registerAPI__ : _function (apiObj)_ - Registers a set of functions.

Examples:

Register a single method

```javascript
aio.register('add', (ctx, a, b) => a+b);
```

Register multiple methods at once

```javascript
aio.registerAPI({
  sqrt        : (ctx, num) => Math.sqrt(num),
  delaySqrt   : (ctx, secs, num) => {
    return new Promise(resolve => {
      setTimeout(() => {
        resolve(Math.sqrt(num))
      }, secs * 1000)
    });
  }
});
```

### Calling functions from the client

There are two ways of doing this.

Use the `call` method:

__call__ : _function (name, args...)_ - Calls the method `name` on the server side passing it `args...` as arguments.

```javascript
let result = await aioClient.call('sqrt', 4);
```

The other one is first call the `update` method wich will map the exposed function in the `remote` object of the instance and also return it:

```javascript
const remote = aioClient.update();

let result = await aioClient.remote.sqrt(4);
// =
let result = await remote.sqrt(4);
```

All remote function wrappers return promise so this is the same as the code above:

```javascript
iaoClient.update()
.then((remote) => {
  return aioClient.remote.sqrt(4);
})
.then((res) => {
  let result = res;
});
```

### Middleware

Awaited.io allows you to set middleware functions on the server side. The middleware chain is executed in the same order it's declared.

When you register a function to be exposed to the client side it is internally attached to the middleware chain, so if you want your middleware to be executed before the response is sent to the client make sure you atach it before registering your exposed functions.

The middleware functions __MUST__ be `async` or return a Promise.

These functions get three arguments:

__next__ : _function (err)_ - This function __MUST ALWAYS__ be called somewhere in the middleware function. If an error is passed the execution of the middleware chain will stop and the error will be passed to the client side which will reject the `Promise` returned by the remote function call.

__ctx__ : _object_ - The shared context

__msg__ : _object_ - The message passed from the client, which will look like:
```javascript
{
  id    : 'dc60f70e-bcb8-4b7c-bf1b-9da460733c8a',
  name  : 'mapSet',
  args  : ['foo', 'bar']
}
```

Example:

Middleware to log the API calls and the time taken to respond
```javascript
aio.use(async (next, ctx, msg) => {
  const now = new Date(); 

  await next(); // Await for the middleware chain to end execution

  const ms = new Date() - now;
  console.log(`-- ${msg.id} - ${msg.name} - ${ms}ms`);
});
```

### Errors

If the `debug` option is set to false, which is the default, errors on your functions will be printed to the console. Otherwise will be sent to the client and thrown there.

### The client is a server and the server is a client

As you may have noticed the same Awaited.io code is used on the __server__ and the __client__ so you can register functions on the client side and call them from the server. The sky is the limit ;)


### Examples

By now there is only one example which is used as a test for all the module functions. It is well commented and I recomend you to read it if you want to know a little more how Awaited.io works.

Also the module code is less than 140 lines of code and is well commented too.

### Coffee contributions accepted

As you may know developers fuel is coffee.

Now that Github enabled **BAT Rewards**, if you are using [Brave Browser](https://brave.com/kal004) and feel that I deserve some coffee you can send me a tip or set a monthly contribution.

If you are still not using [Brave Browser](https://brave.com/kal004), you can download it using [this link]() and I will get some BAT to buy more coffee ;)

[Brave Browser](https://brave.com/kal004) is a superfast browser based in Chrome with integrated ad blocking and anti-tracking features. [Give it a try](https://brave.com/kal004)!

### License

```
Copyright 2017 Albert Calbet Martinez

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
```

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