I am going in circles with following problem, I want to segregate my zmq related code into separate module, I have started my express app in cluster, something like below in my www.js file
if (cluster.isMaster) {
for (; i < cpuLength; i++) {
spawn();
}
}
in my zmq.js file, i want to create a push socket, but i can't bind it to one port since.. well it's a bind not connect, so it gives error after first cluster fork.
i can move my socket code into my www.js file, but then i have to require www file whenever i need to use my socket. i want to keep all zmq related stuff into separate file. any suggestions how to get it working?
More Info. I tried to call bind only once on socket, and keep socket in module/global scope, if i use it to send message, it doesn't throw any error, but i don't recieve any message in my pull socket. Only message i recieve when i send is for process where i called bind, for rest of the process, socket is not null, but it doesn't seem to be doing anything also.
www.js
function startServer() {
if (cluster.isMaster) {
for (; i < cpuLength; i++) {
cluster.fork();
}
cluster.on('online', function (worker) {
log.debug('Worker ' + worker.process.pid + ' is online.');
});
} else {
log.info('Starting server at port ' + config.get('port'));
app.listen(config.get('port'));
}
}
zm.js
if (cluster.isMaster) {
var p = ip + port;
socket.bind(p, function (err) {
if (!err) {
bunyan.info('ZMQ bound ', process.pid);
bunyan.info('ZMQ bound to port', p);
}
});
}
i have tried with binding master, or in forked process, but none seem to be working.
UPDATE: I have got it working for the time being, by binding socket to different ports in sequence with each fork call, but i am sure that's not the best of doing it, ll update if i find a better solution, or if someone else does please let me know.