1.4.4 • Published 3 years ago

pinary v1.4.4

Weekly downloads
2
License
MIT
Repository
github
Last release
3 years ago

pinary


Node.js CI npm version MIT Licence Dependency Status

Introduction

Yet another RPC client and server, with minimalistic publish/subscribe implementation.

  • Server

    • TCP or TLS, as you want (TLS require certificates, see https://github.com/jfromaniello/selfsigned )
    • Persistant connection
    • Binary frames (fast)
    • Optional ZLIB compression
    • Minimalistic JSON Schema implementation (input integrity)
    • Handle Max Clients
    • Basic publish/subscribe support
  • Client

    • Automatic reconnect
    • Internal RPC calls queue
      • Allow lazy client connect
      • Store calls when not connected and play calls when reconnect

Install

npm install pinary

Server

Server: instantiation

const PinaryServer = require('pinary').server;
const server = new PinaryServer();

// with options (see below)
// const server = new PinaryServer({port:64000});
OptionDefaultNotes
useTLSfalseUse clear TCP or TLS
useZLIBfalseUse ZLIB compression
maxClients (1)10Maximum number of simultaneous TCP connections
timeoutData1000Delay before socket close if no data sent, in milliseconds
host0.0.0.0Listening IP/host
port65000 for TCP, 65001 for TLSListening port
keynullTLS: private key
certnullTLS: public key
canullTLS: certificate authority (string of array of string)
secureProtocolTLSv1_2_methodTLS: cipher
rejectUnauthorizedfalseTLS: allow self signed certificates, or not

(1) under the hood, a "client" is in fact 2 sockets, one for writing, one for reading.

Client

Client: instantiation

const PinaryClient = require('pinary').client;
const client = new PinaryClient(); // auto connect

// with a TCP url
// const client = new PinaryClient('tcp://localhost:64000',[options]);

// with a TLS url
// const client = new PinaryClient('tls://localhost:64000',[options]);
OptionDefaultNote
reconnectInterval500milliseconds
queueSize100store rpc calls limit when not connected/disconnected

Client: trigger a method

// using callback
client.rpc('myMethod', (err, result) => {
    if (err) throw err;
    console.log(result);
});

// using async/await
async function letsgo() {
    let result;
    try {
        result = await client.rpcPromise('myMethod');
    } catch(e) {
        // something wrong
    }
    console.log(result);
}

Note: if not yet connected or while the client is trying to reconnect, RPC calls are stored in a queue and played when client is connected.

Client: events

event nameargumentsNotes
connectedretryCountif retryCount = 0, first connection, else reconnection
disconnected
errorError

Publish/Subscribe (PUBSUB)

Client to clients

const Server = require('pinary').server;
const Client = require('pinary').client;

const server = new Server();
const client1 = new Client();
const client2 = new Client();

const channel = '/myChannel';

server.start();

client1.subscribe('/bla', (data) => {
    console.log(data);
    process.exit();
});

client2.publish('/bla', { foo:'bar' });

Server to clients

const Server = require('pinary').server;
const Client = require('pinary').client;

const server = new Server();
const client = new Client();

const channel = '/myChannel';

server.start();

client.subscribe(channel, (data) => {
    console.log(data);
    process.exit();
});

server.publish(channel, { foo:'bar' });

The actual implementation is minimalistic:

  • a channel is considered as an ID, you cannot use wildcards like redis or faye

TODO

  • finish doc
    • events emitted (server)
    • server methods registration (see test/tests/102.methodExist.js, or examples/ for moment)
1.4.4

3 years ago

1.4.3

3 years ago

1.4.2

3 years ago

1.4.1

3 years ago

1.4.0

4 years ago

1.3.9

4 years ago

1.3.8

4 years ago

1.3.7

5 years ago

1.3.6

5 years ago

1.3.5

5 years ago

1.3.4

5 years ago

1.3.3

5 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.9

5 years ago

1.2.8

5 years ago

1.2.7

5 years ago

1.2.6

5 years ago

1.2.5

5 years ago

1.2.4

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago