ion-cortex
Introduction
The "ion-cortex" library is a high-performance communication protocol designed to enable efficient and reliable communication between distributed systems using Redis as the underlying message broker. It simplifies the process of building scalable and resilient microservices.
Installation
To install the "ion-cortex" library, you can use npm:
npm install ion-cortex
Getting Started
To get started with the "ion-cortex" library, require it in your Node.js application:
const Cortex = require("ion-cortex");
Start by creating instance from Cortex
const cortex = new Cortex({
prefix: "app",
url: "redis://localhost:6379",
type: "auth",
});
So as shown above Each instance from Cortex must take 3 necessary arguments
-
prefix
The namespace of the node , as in redis there could be multiple namespaces with unique nodes for each one. -
url
the redis connection url. -
type
node type because you might need multiple nodes from the same type up to achieve a cretin goal ex. High Availability.
after creating the instance you could start exposing the function you want by calling cortex.sub
like
cortex.sub("validateToken", ({ token }) => {
// Do Your Processing Here
return { isValid: true };
});
so now any node in the same namespace could call any exposed function from your service (or as we call it 'node')
cortex.emitToOneOf(
{
type: "auth",
call: "validateToken",
args: { token: "aW9uLWNvcnRleAo=" },
},
async (response) => {
console.log("Got Response", response); // will log `Got Response { isValid: True }` to the console
}
);
This call will choose a random node with type auth
and make a call to the exposed function validateToken
and pass args
to it.
you also have async
version from it:
const response = await cortex.AsyncEmitToOneOf({
type: "jat",
call: "validateToken",
args: { token: "TOKEN" },
});
console.log("Got Response", response); // will log `Got Response { isValid: True }` to the console
Available Functions
-
emitToOneOf
: choose random node with given type. -
AsyncEmitToOneOf
: theasync
version ofemitToOneOf
. -
emitToAllOf
: call all nodes with given type. -
AsyncEmitToAllOf
: theasync
version ofemitToAllOf
. -
emitToOthersOf
: call all nodes with given type except the calling node (useful when sharing data with same nodes mostly in high available setup). -
AsyncEmitToOthersOf
: theasync
version ofemitToOthersOf
. -
emitToOneOfFar
: choose random node with given type but not on the physical machine of the callee. -
AsyncEmitToOneOfFar
: theasync
version ofemitToOneOfFar
. -
emitToAllOfFar
call all nodes with given type but not on the physical machine of the callee. -
AsyncEmitToAllOfFar
: theasync
version ofemitToAllOfFar
. -
waitForNode
: Holds until it discover a given node type with timeout (useful if starting of one node depends on another node).
Migrate From Older Version
There some changes in api from the pervious version
-
No data param in cortex call:
cortex.emitToOneOf( { type: "auth", call: "validateToken", data: { token: "aW9uLWNvcnRleAo=" }, }, async (response) => { console.log("Got Response", response); } );
should become
cortex.emitToOneOf( { type: "auth", call: "validateToken", args: { token: "aW9uLWNvcnRleAo=" }, }, async (response) => { console.log("Got Response", response); } );
-
No Callback
cortex.sub("validateToken", ({ token }, meta, cb) => { // Do Your Processing Here cb({ isValid: true }); });
should become
cortex.sub("validateToken", ({ token }, meta) => { // Do Your Processing Here return { isValid: true }; });
Errors
If the function you are calling crashed or thrown error you will get response like this
// Service
cortex.sub("validateToken", ({ token }) => {
if (!token || token.length < 4) throw new Error("Invalid Token Sent");
// Do Your Processing Here
return { isValid: true };
});
// Calling
const response = await cortex.AsyncEmitToOneOf({
type: "jat",
call: "validateToken",
args: { token: "aW9" },
});
console.log("Got Response", response);
you will get this response
Got response { error: 'Error: Invalid Token Sent' }