Nucleus is flexible library to implement a distributed micro-service(-like) architecture in NodeJS.
Installation
$ npm install idex.nucleus
High level Nucleus architecture
Nucleus implements a two way communication layer based on Redis. Each service are called "Engine" which can communicate
between each other via two different protocols:
Action
An action is similar to an HTTP request: you make a request, you get a response.
const { NucleusEngine } = require('Nucleus');
class DummyEngine extends NucleusEngine {
constructor () {
super('Dummy', {
automaticallyAutodiscover: true,
automaticallyRetrievePendingActions: true
});
}
ping () {
return Promise.resolve({ ping: "Ping" });
}
}
const $dummyEngine = new DummyEngine();
const $testEngine = new NucleusEngine();
Promise.all([ $dummyEngine, $testEngine ])
.then(async () => {
const { ping } = await $testEngine.publishActionByNameAndHandleResponse('Ping', {});
console.log(ping);
});
Event
An event as a similar implementation than a NodeJS EventEmitter event, you can publish/subscribe to an event.
const { NucleusEngine, NucleusEvent } = require('Nucleus');
const $pingEngine = new NucleusEngine();
const $pongEngine = new NucleusEngine();
const $testEngine = new NucleusEngine();
$pingEngine.handleEventByChannelName('Ping', () => {
console.log('PING');
const $event = new NucleusEvent('Pong', {});
process.nextTick($pingEngine.publishEventToChannelByName.bind($pingEngine, 'Pong', $event));
});
$pongEngine.handleEventByChannelName('Pong', () => {
console.log('PONG');
const $event = new NucleusEvent('Ping', {});
process.nextTick($pongEngine.publishEventToChannelByName.bind($pongEngine, 'Ping', $event));
});
$pingEngine.subscribeToChannelName('Ping');
$pongEngine.subscribeToChannelName('Pong');
const $event = new NucleusEvent('Ping', {});
$testEngine.publishEventToChannelByName('Ping', $event);
License
MIT License
Copyright (c) 2018 Sebastien Filion
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.