Booms
A high-performance and easy-to-use RPC microservices framework for Node.js, load a directory as a RPC server, call a remote function or method of a remote object like s1.say.hi(), as same as do it at local. Booms can passes not only data, but also callback functions to the server, that's awesome.
Booms is based on Node.js native TCP socket. It does not require you to write proto files, which is more easier to use.
Server Environment
Booms uses Redis to store the remote functions definition data. If you haven't installed it, please perform the following steps to install it in docker.
1. Install Docker (Docker CE recommended)
2. Install Redis in Docker
1) Install: docker pull redis
2) Start: docker run --restart=always --name redis -d -p 6379:6379 -it redis redis-server
Install
npm install booms --save
TRY IT!
1. Download this repo first
git clone https://github.com/hiowenluke/booms.gitcd boomsnpm install
2. Run examples
Open 4 new tabs in your terminal, then:
1st tab, run `node examples/server1`2nd tab, run `node examples/server2`3rd tab, run `node examples/client`4th tab, run `node examples/client-message-style`
The results in clients will be like below:
Server #1 Server #2
Quick start (in under 3 minutes)
Create a demo directory first.
mkdir ./booms-demo && cd ./booms-demo
1. Server
0) Initialize this server
mkdir ./server && cd ./servernpm init -ynpm install booms --save
1) Run mkdir -p src/say
to create directories, then create file "./src/say/hi.js"
module { return msg: `Hi, I am , years old.`;};
2) Create file "index.js". (See options)
// Create a server named "s1"server;
3) Run
node index.jsServer s1 listening on localhost:50051
2. Client
Open a new tab in your terminal first.
0) Initialize this client
cd .. && mkdir ./client && cd ./clientnpm init -ynpm install booms --save
1) Create file "index.js"
const s1 = ;const main = { const result = await s1say; console;};;
2) Run
node index.js
List of remote functions
The client loads the remote functions definition like below:
const s1 = ;
Click above "booms/services.js" in your editor (such as VS Code or WebStorm) to view it:
const apis = s1: {} {} obj: {} say: {} s2: {} ;...
It will be compact like below while options.isCompactFunctionsList is true. (See options)
// The list of remote functions with parameters. // You should use the "await" keyword to call them.const apis = s1: {} {} obj: do{} say: {} s2: {} ;...
Examples
See examples to learn more.
Call remote object method
In addition to calling a remote function, you can also call a method of a remote object.
1. In server
// obj.jsmoduleexports = async do return `I am obj.do()`; ;
2. In client
const s1 = ;const main = { const result = await s1obj; console; // "I am obj.do()"};;
Passing callback function
Booms can passes not only data, but also callback functions to the server.
1. In server
// callback.jsmodule { // The cb is the callback comes from the client. // The cb has wrapped as an asynchronous function by Booms automatically. // You should use keyword await when invoke it. // The cbResult is the result returned after the cb is executed. const cbResult = await ; return hi + ', ' + cbResult;};
2. In client
const s1 = ; const main = { const x = 1; // 1. The client passes a function to the server via Booms. // 2. The server calls it to get a result, and handles with it. // 3. The server returns the final result to the client. const result = await s1; console; // "hi, 3"}; ;
Calling style
In addition to Object-Style calling like s1.say.hi(), Booms also supports Message-Style calling like call('s1:/say/hi').
Options
Server options
It can be omitted if it is the default value as below.
const booms = ; const options = name: 's1' dir: './src' host: 'localhost' redis: host: 'localhost' ; boomsserver;
Client options
Create file boomsConfig.js under your project root path if needed. It can be omitted if it is the default value as below.
moduleexports = redis: host: 'localhost' // The server names which will be fetched servers: 'all' // For file "booms/services.js" functionList: // If it is true, the function list will be compact like below right. // You should to always use "await" keyword to call these functions. // s1: { s1: { // hi: async function(name, age) {} => hi(name, age) {} // } } isCompact: false // The useArrowFunction is true only takes effect when isCompact is false useArrowFunction: true ;
Test
Download this repo first (see TRY IT) if not yet, then:
npm test
Performance
Booms is based on Node.js native TCP socket, much faster than other RPC frameworks based on gRPC or MQ. (See RPC Frameworks Performance PK)
Why Booms
With Booms, you can require the remote services and call the remote functions as same as you do it at local. That is, you can easily disassemble a project, move any sub module directory to any other location and load it as a microservices any time without adjust any code in parent module.
Why microservices
- Why Enterprises Are Embracing Microservices and Node.js
- Microservices With Node.js: Scalable, Superior, and Secure Apps
License
Copyright (c) 2019, Owen Luke