3

I have multiple nodejs servers located at different locations and i need to create a IPC over tcp sockets and i am using ZeroMQ for that . I need something like request/response or pub/sub in a async way with affirmation that message is sent , but seeing the node-zeromq modules i found all the send methods are synchronous and there is no way to confirm the message the sent through a callback

In short , I need something like socket.send(message,function(err,res){;}); but i found this socket.send(message)

Anyone knows how to do this using ZeroMQ or any other way i could IPC reliablly and with a affirmation as response ?

UPDATE : Ive found https://github.com/visionmedia/axon , Axon and its req/rep send method has a callback , would be great if anyone can shed more light about this .Suggestions ?

4

2 回答 2

0

您可以在 ZMQ 中使用请求/回复模式而不是发布/订阅模式。我相信当你发出请求时,会有一个回调来监听响应,而不是 pub.send()...

于 2015-04-28T20:29:11.567 回答
0

zeromq.node 还不支持send消息的回复回调。

在GitHub 上讨论了几年的一个问题,人们认为这将是一个明智的修改。

我遵循了另一个问题的建议,因为我真的很想在更高级别使用 Promises,因此需要对 REQ/REP 机制进行回调。即从'message'事件处理程序调用回调:

var socket, onRepHandler, replyCallback, send;
socket = zmq.socket('req');
onRepHandler = function (reply) {
    // HACK
    // This handler is a workaround until zeromq.node supports
    // direct callback for REQ/REP:
    // https://github.com/JustinTulloss/zeromq.node/issues/48
    if (replyCallback) {
        replyCallback(reply);
    }
    replyCallback = undefined;
};
socket.on('message', onRepHandler(msg));
socket.connect(address);

// Send method with callback
send = function (msg, repcb) {
    if (replyCallback) {
        throw new Error('Cannot send request before receiving reply of preceding request!');
    }
    replyCallback = repcb;
    socket.send(msg);
}

这感觉像是一个有问题的黑客,但我希望 zeromq.node 库最终得到更新。

于 2016-02-29T17:39:09.450 回答