0

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.

Steve Bennett
  • 114,604
  • 39
  • 168
  • 219
Kamal
  • 1,122
  • 11
  • 18
  • Please include more code, including your www.js file **and** your zmq.js file. If they are too long to include, then reduce them until they still work, but minimally demonstrate your problem. See [here](http://stackoverflow.com/help/mcve) – kdbanman Oct 22 '15 at 19:29
  • 1
    @kdbanman ll this do ? – Kamal Oct 22 '15 at 19:49
  • Much better. It would be useful to see the `require` statements and the variable definitions as well. – kdbanman Oct 22 '15 at 19:59
  • This is an incorrect architecture foundation and a clear indication you should have used different socket types. You "bruteforced" the solution, but with ZMQ there's always an elegant solution. If you still have this issue, can you describe what those push sockets are supposed to do? – Mjh Jan 11 '16 at 12:12
  • @Mjh what do you suggest, i know its brute force, if you can point me into some direction, i can try that – Kamal Feb 29 '16 at 02:11

0 Answers0