1.14.0 • Published 5 years ago

@elementiallabs/sync-sdk v1.14.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

@elementiallabs/sync-sdk

npm (scoped)

Sync SDK for your NodeJS application. This SDK would allow your application to: 1) Send transactions to Broker enabling rest of the peers stay up-to-date with latest transactions across all peers of a network. 2) P2P App to App Communication without getting the broker in the middle (BITM).

Install

$ npm install @elementiallabs/sync-sdk

APIs:

1) writeTX(api_url, payload, (err, resp) => {
    (!err) ? console.log(resp) : console.log(err.state, err.code);
   });
2) readTX(api_url, page, (err, resp) => {
    (!err) ? console.log(resp) : console.log(err.state, err.code);
   })

Usage: Read and Write Transactions

const syncsdk = require('@elementiallabs/sync-sdk');
const config = require('./config');

// First, create a config.json file at app.js/index.js root level
// Second, Config structure to follow:
/*

{
    "peer1": {"alias": "insert your peer name", "ip": "x.x.x.x", "port": "xxxx", "clientid": "insert your client id"},
    "peer2": {"alias": "insert your peer name", "ip": "x.x.x.x", "port": "xxxx", "clientid": "insert your client id"},
    "peer3": {"alias": "insert your peer name", "ip": "x.x.x.x", "port": "xxxx", "clientid": "insert your client id"},
    "peer4": {"alias": "insert your peer name", "ip": "x.x.x.x", "port": "xxxx", "clientid": "insert your client id"},
    "peer5": {"alias": "insert your peer name", "ip": "x.x.x.x", "port": "xxxx", "clientid": "insert your client id"}
}

Refer peer's config file to get alias, ip, port, clientid.

writeTX Payload structure strictly to follow:

const tx = {
    meta: {
        clientid: config.peer1.clientid
    },
    timestamp: Date.now(),
    payload: {
        whatever you want to send ......
    }
}

*/

const { peer1, peer2, peer3, peer4, peer5 ... peerN } = config;

// declare peer variables as much as there are in config.json
const p1 = new syncsdk();
const p2 = new syncsdk();
const p3 = new syncsdk();
const p4 = new syncsdk();
const p5 = new syncsdk();
.
.
.
.
const pN = new syncsdk();

const txPayload = {
    meta: {
        clientid: peer1.clientid
    },
    timestamp: Date.now(),
    payload: {
        xxxxxxx: "xxxxxxxxx",
        body:{
            msg: `Hello ${loremIpsum()}!! Adding new transaction sent from client app!!`
        }
    }
}

p1.writeTX(`http://${peer1.ip}:${peer1.port}/api/sendtxs`, txPayload, (err, resp) => {
    if (!err) {
        console.log(resp);
    } else {
        console.log(err.state, err.code);
    }
});

p2.writeTX(`http://${peer1.ip}:${peer1.port}/api/sendtxs`, txPayload, (err, resp) => {
    if (!err) {
        console.log(resp);
    } else {
        console.log(err.state, err.code);
    }
});

peerName.writeTX(url, payload, (err, resp) => {
    if (!err) {
        console.log(resp);
    } else {
        console.log(err.state, err.code);
    }
});

const page['no'] = 1;

p1.readTX(`http://${peer1.ip}:${peer1.port}/api/fetchtxs`, page.no, (err, resp) => {
    if (!err) {
        console.log(resp);
    } else {
        console.log(err.state, err.code);
    }
});

p2.readTX(`http://${peer1.ip}:${peer1.port}/api/fetchtxs`, page.no, (err, resp) => {
    if (!err) {
        console.log(resp);
    } else {
        console.log(err.state, err.code);
    }
});


peerName.readTX(url, pageNumber, (err, resp) => {
    if (!err) {
        console.log(resp);
    } else {
        console.log(err.state, err.code);
    }
});

Usage: Send and Receive Intra-Peer Messages

Peer 1 Client try to send message to Peer 2

const p1MessageConfig = {
    type: config.peer1.clientid,
    msg: 'Hey there Peer 2 !!!!',
    from: config.peer1.alias
    to: config.peer2.alias
}

p1.sendMessage(p1MessageConfig)
.then(response => {
    console.log(response);
});

p2.listener(config.peer2.clientid, config.peer2.alias)
.then(objct => {
    if(objct.msg) {
        console.log(objct.msg);
        objct.cb(null, 'whatever reply you want to pass');
    }
})
1.14.0

5 years ago

1.13.0

5 years ago

1.12.0

5 years ago

1.11.0

5 years ago

1.10.0

5 years ago

1.9.0

5 years ago

1.8.0

5 years ago

1.7.0

5 years ago

1.6.0

5 years ago

1.5.0

5 years ago

1.4.0

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago