ion-cortex v2.1.5
Cortex
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
- Node.js
- 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,
}
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
10 months ago
10 months ago
9 months ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago