Zeronode - minimal building block for NodeJS microservices
- Why Zeronode?
- Installation
- Basics
- Benchmark
- API
- Examples
- Advanced
- Contributing
- Have a question ?
- License
Why you need ZeroNode ?
Application backends are becoming complex these days and there are lots of moving parts talking to each other through network. There is a great difference between sending a few bytes from A to B, and doing messaging in reliable way.
- How to handle dynamic components ? (i.e., pieces that come and/or go away temporarily, scaling a microservice instances )
- How to handle messages that we can't deliver immediately ? (i.e waiting for a component to come back online)
- How to route messages in complex microservice architecture ? (i.e. one to one, one to many, custom grouping)
- How we handle network errors ? (i.e., reconnecting of various pieces)
We created Zeronode on top of zeromq as to address these
and some more common problems that developers will face once building solid systems.
With zeronode its just super simple to create complex server-to-server communications (i.e. build network topologies).
Installation & Important notes
Zeronode depends on zeromq
For Debian, Ubuntu, MacOS you can just run
$ npm install zeronode --save
and it'll also install zeromq for you.
Kudos to Dave for adding install scripts.
For other platforms please open an issue or feel free to contribute.
Basics
Zeronode allows to create complex network topologies (i.e. line, ring, partial or full mesh, star, three, hybrid ...) Each participant/actor in your network topology we call znode, which can act as a sever, as a client or hybrid.
; let znode = id: 'steadfast' options: {} config: {}; // ** If znode is binded to some interface then other znodes can connect to it// ** In this case znode acts as a server, but it's not limiting znode to connect also to other znodes (hybrid)async { await znode;}; // ** znode can connect to multiple znodesznodeznode // ** If 2 znodes are connected together then we have a channel between them // ** and both znodes can talk to each other via various messeging patterns - i.e. request/reply, tick (fire and forgot) etc ...
Much more interesting patterns and features you can discover by reading the API document. In case you have a question or suggestion you can talk to authors on Zeronode Gitter chat
Benchmark
All Benchmark tests are completed on Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz.
Zeronode | Seneca (tcp) | Pigato | |
1000 msg, 1kb data | 394ms | 2054ms | 342ms |
50000 msg, 1kb data | 11821ms | 140934ms | FAIL(100s timeout) |
API
Basic methods
Simple messaging methods
Attaching/Detaching handlers to tick and request
Load balancing methods
znode.requestAny()
znode.requestDownAny()
znode.requestUpAny()
znode.tickAny()
znode.tickDownAny()
znode.tickUpAny()
znode.tickAll()
znode.tickDownAll()
znode.tickUpAll()
Debugging and troubleshooting
let znode = new Node({ id: String, bind: Url, options: Object, config: Object })
Node class wraps many client instances and one server instance. Node automatically handles:
- Client/Server ping/pong
- Reconnections
; let znode = id: 'node' bind: 'tcp://127.0.0.1:6000' options: {} config: {};
All four arguments are optional.
id
is unique string which identifies znode.options
is information about znode which is shared with other connected znoded. It could be used for advanced use cases of load balancing and messege routing.config
is an object for configuring znodelogger
- logger instance, default is Winston.REQUEST_TIMEOUT
- duration after which request()-s promise will be rejected, default is 10,000 ms.RECONNECTION_TIMEOUT
(for client znodes) - zeronode's default is -1 , which means zeronode is always trying to reconnect to failed znode server. OnceRECONNECTION_TIMEOUT
is passed and recconenction doesn't happen zeronode will fireSERVER_RECONNECT_FAILURE
.CONNECTION_TIMEOUT
(for client znodes) - duration for trying to connect to server after which connect()-s promise will be rejected.
There are some events that triggered on znode instances:
-
NodeEvents.
CLIENT_FAILURE
- triggered on server znode when client connected to it fails. -
NodeEvents.
CLIENT_CONNECTED
- triggered on server znode when new client connects to it. -
NodeEvents.
CLIENT_STOP
- triggered on server znode when client successfully disconnects from it. -
NodeEvents.
SERVER_FAILURE
- triggered on client znode when server znode fails. -
NodeEvents.
SERVER_STOP
- triggered on client znode when server successfully stops. -
NodeEvents.
SERVER_RECONNECT
- triggered on client znode when server comes back and client znode successfuly reconnects. -
NodeEvents.
SERVER_RECONNECT_FAILURE
- triggered on client znode when server doesn't come back inreconnectionTimeout
time provided during connect(). IfreconnectionTimeout
is not provided it usesconfig.RECONNECTION_TIMEOUT
which defaults to -1 (means client znode will try to reconnect to server znode for ages). -
NodeEvents.
CONNECT_TO_SERVER
- triggered on client znode when it successfully connects to new server. -
NodeEvents.
METRICS
- triggered when metrics enabled.
znode.bind(address: Url)
Binds the znode to the specified interface and port and returns promise.
You can bind only to one address.
Address can be of the following protocols: tcp
, inproc
(in-process/inter-thread), ipc
(inter-process).
znode.connect({ address: Url, timeout: Number, reconnectionTimeout: Number })
Connects the znode to server znode with specified address and returns promise.
znode can connect to multiple znodes.
If timeout is provided (in milliseconds) then the connect()-s promise will be rejected if connection is taking longer.
If timeout is not provided it will wait for ages till it connects.
If server znode fails then client znode will try to reconnect in given reconnectionTimeout
(defaults to RECONNECTION_TIMEOUT
) after which the SERVER_RECONNECT_FAILURE
event will be triggered.
znode.unbind()
Unbinds the server znode and returns promise.
Unbinding doesn't stop znode, it can still be connected to other nodes if there are any, it just stops the server behaviour of znode, and on all the client znodes (connected to this server znode) SERVER_STOP
event will be triggered.
znode.disconnect(address: Url)
Disconnects znode from specified address and returns promise.
znode.stop()
Unbinds znode, disconnects from all connected addresses (znodes) and returns promise.
znode.request({ to: Id, event: String, data: Object, timeout: Number })
Makes request to znode with id(to) and returns promise.
Promise resolves with data that the requested znode replies.
If timeout is not provided it'll be config.REQUEST_TIMEOUT
(defaults to 10000 ms).
If there is no znode with given id, than promise will be rejected with error code ErrorCodes.NODE_NOT_FOUND
.
znode.tick({ to: Id, event: String, data: Object })
Ticks(emits) event to given znode(to).
If there is no znode with given id, than throws error with code ErrorCodes.NODE_NOT_FOUND
.
znode.onRequest(requestEvent: String/Regex, handler: Function)
Adds request handler for given event on znode.
/*** @param head: { id: String, event: String }* @param body: * @param reply(replyData: Object): Function* @param next(error): Function */// ** listening for 'foo' eventznode // ** listening for any events matching Regexpznode
znode.onTick(event: String/Regex, handler: Function)
Adds tick(event) handler for given event.
znode
znode.offRequest(requestEvent: String/Regex, handler: Function)
Removes request handler for given event.
If handler is not provided then removes all of the listeners.
znode.offTick(event: String/Regex, handler: Function)
Removes given tick(event) handler from event listeners' list.
If handler is not provided then removes all of the listeners.
znode.requestAny({ event: String, data: Object, timeout: Number, filter: Object/Function, down: Bool, up: Bool })
General method to send request to only one znode satisfying the filter.
Filter can be an object or a predicate function. Each filter key can be object itself, with this keys.
- $eq - strict equal to provided value.
- $ne - not equal to provided value.
- $aeq - loose equal to provided value.
- $gt - greater than provided value.
- $gte - greater than or equal to provided value.
- $lt - less than provided value.
- $lte - less than or equal to provided value.
- $between - between provided values (value must be tuple. eg [10, 20]).
- $regex - match to provided regex.
- $in - matching any of the provided values.
- $nin - not matching any of the provided values.
- $contains - contains provided value.
- $containsAny - contains any of the provided values.
- $containsNone - contains none of the provided values.
// ** send request to one of znodes that have version 1.*.* znode // ** send request to one of znodes whose version is greater than 1.0.0 znode // ** send request to one of znodes whose version is between 1.0.0 and 2.0.0 znode // ** send request to one of znodes that have even length of name. znode // ** send request to one of znodes that connected to your znode (downstream client znodes) znode // ** send request to one of znodes that your znode is connected to (upstream znodes). znode
znode.requestDownAny({ event: String, data: Object, timeout: Number, filter: Object/Function })
Send request to one of downstream znodes (znodes which has been connected to your znode via connect() ).
znode.requestUpAny({ event: String, data: Object, timeout: Number, filter: Object/Function })
Send request to one of upstream znodes (znodes to which your znode has been connected via connect() ).
znode.tickAny({ event: String, data: Object, filter: Object/Function, down: Bool, up: Bool })
General method to send tick-s to only one znode satisfying the filter.
Filter can be an object or a predicate function.
Usage is same as node.requestAny
znode.tickDownAny({ event: String, data: Object, filter: Object/Function })
Send tick-s to one of downstream znodes (znodes which has been connected to your znode via connect() ).
znode.tickUpAny({ event: String, data: Object, filter: Object/Function })
Send tick-s to one of upstream znodes (znodes to which your znode has been connected via connect() ).
znode.tickAll({ event: String, data: Object, filter: Object/Function, down: Bool, up: Bool })
Tick to ALL znodes satisfying the filter (object or predicate function), up ( upstream ) and down ( downstream ).
znode.tickDownAll({ event: String, data: Object, filter: Object/Function })
Tick to ALL downstream znodes.
znode.tickUpAll({ event: String, data: Object, filter: Object/Function })
Tick to ALL upstream znodes.
znode.enableMetrics(interval)
Enables metrics, events will be triggered by the given interval. Default interval is 1000 ms.
znode.disableMetrics()
Stops triggering events, and removes all collected data.
Examples
Simple client server example
NodeServer is listening for events, NodeClient connects to NodeServer and sends events:
(myServiceClient) ----> (myServiceServer)
Lets create server first
myServiceServer.js
; { let myServiceServer = id: 'myServiceServer' bind: 'tcp://127.0.0.1:6000' options: layer: 'LayerA' ; // ** attach event listener to myServiceServer myServiceServer; // ** attach request listener to myServiceServer myServiceServer; // second handler for same channel myServiceServer; // ** bind znode to given address provided during construction await myServiceServer;};
Now lets create a client
myServiceClient.js
{ let myServiceClient = options: layer: 'LayerA' ; //** connect one node to another node with address await myServiceClient; let serverNodeId = 'myServiceServer'; // ** tick() is like firing an event to another node myServiceClient; // ** you request to another node and getting a promise // ** which will be resolve after reply. let responseFromServer = await myServiceClient; console; // ** response from server is "Hello client."};
Example of filtering the znodes via options.
Let's say we want to group our znodes logicaly in some layers and send messages considering that layering.
- znode-s can be grouped in layers (and other options) and then send messages to only filtered nodes by layers or other options.
- the filtering is done on senders side which keeps all the information about the nodes (both connected to sender node and the ones that sender node is connected to)
In this example, we will create one server znode that will bind in some address, and three client znodes will connect to our server znode.
2 of client znodes will be in layer A
, 1 in B
.
serverNode.js
{ let server = bind: 'tcp://127.0.0.1:6000' ; await server;};
clientA1.js
{ let clientA1 = options: layer: 'A' ; clientA1; // ** connect to server address and set connection timeout to 20 seconds await clientA1;};
clientA2.js
{ let clientA2 = options: layer: 'A' ; clientA2; // ** connect to server address and set connection timeout infinite await clientA2;
clientB1.js
{ let clientB1 = options: layer: 'B' ; clientB1; // ** connect to server address and set connection timeout infinite await clientB1;};
Now that all connections are set, we can send events.
// ** this will tick only one node of the layer A nodes;server; // ** this will tick to all layer A nodes;server; // ** this will tick to all nodes that server connected to, or connected to server.server; // ** you even can use regexp to filer znodes to which the tick will be sent// ** also you can pass a predicate function as a filter which will get znode-s options as an argumentserver
Still have a question ?
We'll be happy to answer your questions. Try to reach out us on zeronode gitter chat
Contributing
Contributions are always welcome!
Please read the contribution guidelines first.
Contributors
More about zeronode internals
Under the hood we are using zeromq-s Dealer and Router sockets.