I want to send ZMQ message inside app.get but when running I throw error:
events.js:72
throw er; // Unhandled 'error' event
^
ReferenceError: res is not defined
My running code:
var zmq = require('zmq'),
zreq = zmq.socket('req'),
app = express();
zreq.connect('tcp://localhost:5559');
zreq.on('message', function (msg) {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.send(msg);
});
app.get('/words', function (req, res) {
zreq.send('nodejs');
//I think it should have something like zreqCallback(req)?
});
The problem is, as the error indicates, that you don't have access to the res object from the message event handler on your req socket. What you need is a way to link the message in that event handler to your res object. You're not going to be able to do that (easily) without support from the other end of that socket.
The basic idea is to associate a unique id with each message you send over zmq and include it in the message sent through the req socket and the reply that comes back from the rep socket. Then also associate the res object with the same message id.
I usually use node-uuid for unique id generation. You'll also need a way to easily encode / decode your messages (it looks like you're just sending straight strings at the moment). The built in JSON parser works fine for that, or you could use something more compact like bencode or protobuf. Be sure to pick something that both ends of the socket can work with.
You're code would look something like this:
Note: I'm assuming we're using node-uuid and JSON. Also, I'm not putting any error handling or sanity checks in here; don't forget that stuff.
var zmq = require('zmq'),
uuid = require('node-uuid'),
zreq = zmq.socket('req'),
app = express();
var responses = {};
zreq.connect('tcp://localhost:5559');
zreq.on('message', function (data) {
data = JSON.parse(data);
var msgId = data.id;
var res = responses[msgId];
res.writeHead(200, { 'Content-Type': 'application/json' });
res.send(data.message);
responses[msgId] = null;
});
app.get('/words', function (req, res) {
var msgId = uuid.v4();
var data = { id: msgId, message: 'nodejs' };
responses[msgId] = res;
zreq.send(JSON.stringify(data));
});
On the other end (I'll just assume it's also written in node for the purpose of this example):
var zmq = require('zmq');
zrep = zmq.socket('rep');
zrep.bind('tcp://localhost:5559');
zrep.on('message', function(data) {
data = JSON.parse(data);
var msgId = data.id;
var msg = data.message;
// Do whatever you were doing before to process the message
// We'll say it ends up in a "results" variable
var response = { id: msgId, message: results };
zrep.send(JSON.stringify(response));
});