!!!!! DEPRECATED REPOSITORY !!!!! PLEASE SWITCH TO PIGATO
ZeroMQ Obsessive Majordomo Protocol: microservices framework for NodeJS - reliable and extensible service-oriented request-reply inspired by ZeroMQ Majordomo Protocol (MDP) v0.2.
- Worker : receives requests, does something and replies. A worker offers a service, should be a functionality as atomic as possible
- Client : creates, pushes requests and waits for results (if needed). A request always includes a service and a payload/data for the Worker
- Broker : handles requests queueing and routing
Worker receives "request"
events that contain 2 arguments:
data
- value sent by a client for this request.reply
- extended writable stream to send data to the client.
reply
writable stream exposes also following methods:
write
- sends partial data to the client (triggers partial callback). It is used internally to implement writable streams.end
- sends last data to the client (triggers final callback) and completes/closes current request. Use this method for single-reply requests.heartbeat
- forces sending heartbeat to the broker and clientactive
- returns (boolean) the status of the request. A request becomes inactive when the worker disconnects from the broker or it is discarded by the client or the client disconnects from the broker. This is useful for long running tasks and Worker can monitor whether or not continue processing a request.ended
- returns (boolean) if the request has been ended, so the worker cannot push more data to the client.
worker.on('request', function(data, reply) {
fs.createReadStream(data).pipe(reply);
});
// or
worker.on('request', function(data, reply) {
for (var i = 0; i < 1000; i++) {
res.write('PARTIAL DATA ' + i);
}
res.end('FINAL DATA');
});
Take note: due to the framing protocol of zmq
only the data supplied to response.end(data)
will be given to the client's final callback.
Clients may make simple requests using client.request(...)
with 5 arguments.
serviceName
- name of the service we wish to connect todata
- data to give to the servicepartialCallback(data)
- called whenever the request does not end but emits datafinalCallback(err, data)
- called when the request will emit no more dataopts
- options object for the request
client.request('echo', 'data', function (data) {
// frames sent prior to final frame
console.log('partial data', data);
}, function (err, data) {
// this is the final frame sent
console.log('final data', data);
}, { timeout: 5000 });
Clients may also make streaming request using client.requestStream()
with 3 arguments.
serviceName
data
opts
client.requestStream('echo', 'data', { timeout: -1 }).pipe(process.stdout);
timeout
: default 60000 (60 seconds). Set -1 to disable
Simply starts up a broker.
Take note: when using a inproc
socket the broker must become active before any queued messages.
- Reliable request / reply protocol
- Scalability
- Multi-Worker : infinite services and infinite workers for each service
- Multi-Client : infinite clients
- Multi-Broker : infinite brokers to avoid bottlenecks and improve network reliability
- Compatibility with MDP protocol v0.2 .
- Support for partial replies.
- Client multi-request support.
- Client heartbeating for active requests. Allows Workers to dected whenever Clients disconnect or lose interest in some request. This feature is very useful to stop long-running partial requests (i.e data streaming) allowing Worker to be requeued by the Broker for new tasks.
- Worker / Broker heartbeating for active / in-progress requests. Allow Clients to detect whenever a Worker quits or dies while processing a Client request.
- Worker <-> Broker heartbeating.
- Broker MAY track Worker/Client/Request relation.
- Client MAY send heartbeat for active request. If the request is being processed by Worker, Broker forwards heartbeat to Worker.
- Worker MAY decide to stop an inactive Request (tracks liveness for Request).
- Client MAY assign a timeout to a Request.
- Worker SHALL NOT send more W_REPLY (for a Request) after sending first W_REPLY message.
- Broker SHALL force disconnect Broker if any error occurs.
- Add authentication support through zmq-zap ZeroMQ ZAP to trust Clients and Workers.
- Fincluster - cloud financial platform : fincluster / @fincluster
- My personal blog : ardoino.com / @paoloardoino
Based on https://github.com/nuh-temp/zmq-mdp2 project