xbus
v1.1.3
Published
simple microservice bus
Downloads
10
Readme
#Run xbus server
- Use ./bin/xbus script to run mircro-services server
- Use Service class
const { Server } = require('xbus');
const server = new Server();
server.listen();
Server listen on 7011 port
#Simple chat
const { Service } = require('xubs');
const chat = new Service('chat');
const user = 'Jim';
chat.on('message', message => {
console.log(`${message.user}: ${message.text}`);
});
process.stdin.on('data', data => {
const text = data.toString().trimRight();
chat.emit('message', {user, text});
});
#Service
##Publish-Subscribe (Events)
###new Service(name [, target])
const { Service } = require('xbus');
const service = Service('my-servise');
###service.on(type, subscriber)
service.on('add-user', function(user){
console.log(user);
});
###service.off(type, subscriber)
// add subscriber
service.on('add-user', console.log);
// remove subscriber
service.off('add-user', console.log);
###service.emit(type, event)
const user = {id: 1, name: 'Jim'};
service.emit('add-user', user);
##Request-Response
###service.listen(type, listener) Sync listener:
service.listen('sum', function({a, b}){
return a + b;
});
Async listener:
const fs = require('fs');
const fsPromises = fs.promises;
service.listen('readFile', async function(fileName){
const contents = await fsPromises.readFile(fileName);
return contents;
});
###service.stop(type, listener)
function sum({a, b}){
return a + b;
}
// add listener
service.listen('sum', sum);
// remove listener
service.stop('sum', sum);
###async service.send(type, request)
const userId = 12;
service.send('getUser', userId)
.then(function(user){
console.log(user);
})