2.1.5 • Published 5 months ago

ion-cortex v2.1.5

Weekly downloads
-
License
ISC
Repository
-
Last release
5 months ago

Cortex

https://images.unsplash.com/photo-1530973428-5bf2db2e4d71?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb

Why we designed Cortex

Cortex is a communication protocol built to allow different nodes in different machines to call each other executing the exposed function in each node very fast and efficiently.

Technologies used to build Cortex

  1. Node.js
  2. Redis

Create Cortex instance

// require cortex package
const Cortex = require('ion-cortex');

/**
* 
* @param { string }   prefix - teh name space which the node exists in.
* @param { string }   url - redis database url in which the node will be connected..
* @param { string }   type - the type of the node.
* @param { function } state - a function invoked everty second sharing data with the network.
* @param { function } defaultTimeout - if set the cortex will wait for that number of milliseconds before timing out.
*
*/

const cortex = new Cortex({
        prefix: "space",
        url: "redis://165.22.65.7:6379",
        type: 'm1',
        state: ()=>{
            return { sharedData: "some data"} 
        }
    });

Functions

emit

Used to call a function in an CERTAIN node using this node id , passing some data

// emit({to, call, data, reply}, cb, timeout);
/**
 * 
 * @param { string }   to - the id of the node you want to call.
 * @param { string }   call - name of the function to be called.
 * @param { string }   data - data that will be sent to the called function.
 * @param { string }   reply - object contain the id of the node and the function you want to call within this specfied node on reciving this call.
 * @param { function } cb - the function to be executed after calling the node. 
 * @param { number }  timeout (optional)- the time in ms to timeout after calling the node with no response. 
 * 
 */

cortex.emit({
            to: 'node id',
            call: "functionName",
            data: {
                name: "max",
								age: 28
            },
            reply: {
                to: cortex.id,
                call: "otherFunction"
            }
    }, 
    (data)=>{
        if(data.error){
            console.log(`RPC ERROR`, data.error);
        } else {
            console.log(`*** reached the node and returning ***`)
            console.log(data);
        }
})

emitToOneOf

Allow node to call a certain function from ANY one of the other nodes having the same specified type.

This method will be load balanced between all nodes having the same specified type.

// emitToOneOf({type, call, data, reply}, cb, timeout);
/**
 * 
 * @param { string }   type - type of node you want to call.
 * @param { string }   call - name of the function to be called.
 * @param { string }   data - data that will be sent to the called function.
 * @param { string }   reply - object contain the id of the node and the function you want to call within this specfied node on reciving this call.
 * @param { function } cb - the function to be executed after calling the node. 
 * @param { number }  timeout (optional)- the time in ms to timeout after calling the node with no response. 
 * 
 */

cortex.emitToOneOf({
            type: 'nodeType',
            call: "functionName",
            data: {
                name: "max",
								age: 28
            },
            reply: {
                to: cortex.id,
                call: "otherFunction"
            }
    }, 
    (data)=>{
        if(data.error){
            console.log(`RPC ERROR`, data.error);
        } else {
            console.log(`*** reached the node and returning ***`)
            console.log(data);
        }
})

Note: reply object will be neglected in case of providing a callback function (cb) and the callback function will be executed automatically after the call.

emitToOneOfFar

Allow node to call a certain function from ANY one of the other nodes having the same specified type. but on other machines

This method will be load balanced between all nodes having the same specified type.

// emitToOneOfFar({type, call, data, reply}, cb, timeout);
/**
 * 
 * @param { string }   type - type of node you want to call.
 * @param { string }   call - name of the function to be called.
 * @param { string }   data - data that will be sent to the called function.
 * @param { string }   reply - object contain the id of the node and the function you want to call within this specfied node on reciving this call.
 * @param { function } cb - the function to be executed after calling the node. 
 * @param { number }  timeout (optional)- the time in ms to timeout after calling the node with no response. 
 * 
 */

cortex.emitToOneOfFar({
            type: 'nodeType',
            call: "functionName",
            data: {
                name: "max",
								age: 28
            },
            reply: {
                to: cortex.id,
                call: "otherFunction"
            }
    }, 
    (data)=>{
        if(data.error){
            console.log(`RPC ERROR`, data.error);
        } else {
            console.log(`*** reached the node and returning ***`)
            console.log(data);
        }
})

Note: reply object will be neglected in case of providing a callback function (cb) and the callback function will be executed automatically after the call.

emitToAllOf

Allow node to call a certain function in All the other nodes having the same specified type

emitToAllOf({type, call, data, reply}, cb, timeout);
/**
   * 
   * @param { string }   type - type of nodes you want to call.
   * @param { string }   call - name of the function to be called.
   * @param { string }   data - data that will be sent to the called function.
   * @param { string }   reply - object contain the id of the node and the function you want to call within this specfied node on reciving this call.
   * @param { function } cb - the function to be executed after calling the node. 
   * @param { number }  timeout (optional)- the time in ms to timeout after calling the node with no response. 
   * 
   */

cortex.emitToAllOf({
            type: 'nodeType',
            call: "functionName",
            data: {
                name: "max",
								age: 28
            },
            reply: {
                to: cortex.id,
                call: "otherFunction"
            }
    }, 
    (data)=>{
        if(data.error){
            console.log(`RPC ERROR`, data.error);
        } else {
            console.log(`*** reached the node and returning ***`)
            console.log(data);
        }
})

Note: reply object will be neglected in case of providing a callback function (cb) and the callback function will be executed automatically after the call.

emitToAllOfFar

Allow node to call a certain function in All the other nodes having the same specified type but on other machines

emitToAllOfFar({type, call, data, reply}, cb, timeout);
/**
   * 
   * @param { string }   type - type of nodes you want to call.
   * @param { string }   call - name of the function to be called.
   * @param { string }   data - data that will be sent to the called function.
   * @param { string }   reply - object contain the id of the node and the function you want to call within this specfied node on reciving this call.
   * @param { function } cb - the function to be executed after calling the node. 
   * @param { number }  timeout (optional)- the time in ms to timeout after calling the node with no response. 
   * 
   */

cortex.emitToAllOfFar({
            type: 'nodeType',
            call: "functionName",
            data: {
                name: "max",
								age: 28
            },
            reply: {
                to: cortex.id,
                call: "otherFunction"
            }
    }, 
    (data)=>{
        if(data.error){
            console.log(`RPC ERROR`, data.error);
        } else {
            console.log(`*** reached the node and returning ***`)
            console.log(data);
        }
})

Note: reply object will be neglected in case of providing a callback function (cb) and the callback function will be executed automatically after the call.

sub

Allow the node to expose a certain event to be called by the other nodes

// sub(event, cb)
/**
 * 
 * @param { string }   event - name of the event you will expose to other nodes.
 * @param { function } cb - the function to be executed after calling the node type 
 *													with the event name and you can acces the data sent 
 *													by the node calling this event. 
 * 
 */

cortex.sub("eventName", (data) =>{
    console.log(data)
    return "something"
 });

unsub

Allow the node to execlude a certain event and not to listen on it anymore.

// unsub(event)
/**
 * 
 * @param { string }   event - name of event you want not to listen on it anymore.
 *
*/

cortex.sub("eventName");

advanced example

require('dotenv').config()


const Cortex         = require('./index');

const {
    performance
  } = require('perf_hooks');


(async()=>{

    const cortex = new Cortex({
        prefix: "ionx",
        url: "redis://127.0.0.1:6379",
        type: 'sfu',
        state: ()=>{
            return {
                cors: 12
            }
        }
    });
    cortex.sub("_fojo_", (d)=>{
            console.log(`👯`,d);
        }
    );
    cortex.sub("nested.*",(d, meta)=>{
        if(d.error){
            console.log(d.error);
        }
        console.log('got meta', meta);
        let value = 0;
        if(!d.numbers){
            throw Error(`missing number`);
        } else {
            d.numbers.forEach(i=>{
                value=value+i});
        }
        return {value};
    })

    
    setInterval(()=>{
        var t0 = performance.now()
        cortex.emitToOneOf({
            type: 'sfu',
            call: "nested._mojo_",
            data: {
                numbers: [1,3,4],
            },
            reply: {
                to: cortex.id,
                call: '_fojo_',
            }
    }, 
    (data)=>{
        var t1 = performance.now();
        console.log(`time: `, t1-t0);
        if(data.error){
            console.log(`RPC ERROR`, data.error);
        } else {
            console.log(`RPC su----->`, data);
        }
    }
    )}, 500);
 
    
})();

Cortex Instance Params

{
        type,
        prefix, 
        url, 
        producerDefaultMaxLen,
        stateMaxLen,
        state,
        useHostNameAsId,
        activeDelay,
        idlDelay,
    }
2.1.2

5 months ago

2.1.4

5 months ago

2.1.3

5 months ago

2.1.6

5 months ago

2.1.5

5 months ago

2.0.1

10 months ago

2.0.0

10 months ago

2.1.1

9 months ago

1.5.3

1 year ago

1.5.2

1 year ago

1.5.1

1 year ago

1.4.6

2 years ago

1.4.5

2 years ago

1.4.4

2 years ago

1.5.0

1 year ago

1.4.9

2 years ago

1.4.8

2 years ago

1.4.7

2 years ago

1.4.3

2 years ago

1.4.2

2 years ago

1.4.1

2 years ago

1.4.0

2 years ago

1.3.9

2 years ago

1.3.8

2 years ago

1.3.7

2 years ago

1.3.6

2 years ago

1.3.5

2 years ago

1.2.8

2 years ago

1.2.7

2 years ago

1.3.4

2 years ago

1.3.3

2 years ago

1.3.2

2 years ago

1.3.1

2 years ago

1.3.0

2 years ago

1.2.9

2 years ago

1.2.6

2 years ago

1.2.5

3 years ago

1.2.4

3 years ago

1.2.3

3 years ago

1.2.0

3 years ago

1.1.9

3 years ago

1.1.8

3 years ago

1.1.7

3 years ago

1.2.2

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.1

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.0

3 years ago

1.0.9

3 years ago

1.0.10

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago