ZeroMQ Obsessive Majordomo Protocol: microservices framework for NodeJS - reliable and extensible service-oriented request-reply inspired by ZeroMQ Majordomo Protocol (MDP) v0.2.
omdp.Worker(socket_str, service_name)
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.
omdp.Client(socket_str)
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 requestclient.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 disableomdp.Broker(socket_str)
Simply starts up a broker.
Take note: when using a inproc
socket the broker must become active before any queued messages.
Based on https://github.com/nuh-temp/zmq-mdp2 project