Microcore.js is a library for simple creation of pipelinening microservices in Node.js with RabbitMQ.
npm install --save microcore
Since Microcore.js is written in ES6 and it uses async/await - it requires latest stable node (7.x or later).
- Simple interface for building pipelinening (micro)services
- Easy way to scale services both horizontally (by adding more nodes) and vertically (by adding more subscribers)
Example service that subscribe to messages from helloworld
topic and does some work with incoming data (in this case it just appends world!
to incoming string):
const createService = require('microcore');
// service config
const serviceConfig = {
ID: 'helloworld',
type: 'servicetype',
rabbit: {host: 'rabbit', exchange: 'exchange'},
statusReportInterval: 60000,
resultKey: 'responsekey',
};
// creating service will return shutdown function
const shutdown = await createService({
config: serviceConfig,
onInit() {
// triggered on service init
console.log('Hello world service started!');
},
onJob(data, done) {
// triggered on incoming data to process.
// do your work here..
const resultData = `${data} world!`;
// call `done(err, result)` after doing the work with data
// result will be sent to `resultKey`
done(err, resultData);
// alternatively you can specify responseKey, e.g.:
// done(err, resultData, responseKey);
},
onCleanup() {
// triggered before service cleanup
console.log('Hello world service stopping!');
},
});
Example service that sends messages to helloworld
and logs response to console:
const Microwork = require('microwork');
// create master
const master = new Microwork({host: 'rabbit', exchange: 'exchange'});
// listen for reply from workers
await master.subscribe('responsekey', msg => {
console.log(msg); // -> "hello world!"
});
// send message to workers
await master.send('helloworld', 'hello');