zeromq.node icon indicating copy to clipboard operation
zeromq.node copied to clipboard

bindSync, bind, connect question

Open adamwlarson opened this issue 14 years ago • 2 comments

I am working on a prototype for a distributed system that will use zeromq and node.js.

I actually have 2 questions I hope someone would be able to help me with

First: I was wondering if there is further explanation of when to use bindSync, bind, and connect. Right now i'm using bindSync for any push sockets since it is the only way I can get those to work. Connect and bind both work on the pull socket but do not work on the push socket. Is there some additional documentation I can be pointed to?

Then... I want to make it where I can start up various servers all running a node cluster, where the master process has a push socket, and the workers have a pull socket.

var cluster = require('cluster')
  , zmq = require( 'zmq' )
  , numWorkers = 4
  , pull_port = "tcp://127.0.0.1:5000"
  , push_port = "tcp://127.0.0.1:5001";


if( cluster.isMaster ) {
  //Create push socket
  var upStream = zmq.socket( 'push' );

  for (var i = 0; i < numWorkers; i++) {
    var worker = cluster.fork();
    worker.on( 'message', function( msg ) {
      if( msg.data ){
        console.log( "Sending to Upstream" + " " + msg.data.toString( ) );
        upStream.send( msg.data );
      }
    });
  } 
  console.log( "Starting Workers" );
  cluster.on('death', function(worker) {
    console.log('worker ' + worker.pid + ' died');
  });

  upStream.bindSync( push_port ); 
} else {
  var downStream = zmq.socket( 'pull' );
  downStream.connect( pull_port );
  console.log( "Worker Started " + process.pid );

  downStream.identity = 'downstream' + process.pid;

  downStream.on('message', function(data) {
    console.log(downStream.identity + ': received data ' + data.toString());
    process.send( { data: data + " " + downStream.identity.toString( ) });
  });
}

The push socket is the problem, if I do a bindSync to the push_port location, I can no longer start a second instance of the worker cluster (since the address is now already in use), is there a way around that or is there a preferred socket type or setup for what I'm trying to do?

I really just need to take info in and have it processed along multiple processes, then when it finishes I need to send it out to a "sink". I originally had it sent from each of the individual processes but because of the push socket only one process could actually "push" the data.

I'm fairly new to zeromq, but I have read through the guide, and I've done quite a lot of socket level programming for windows, and video game consoles over the last 10 years, but zeroMQ is a different way of thinking about things so go easy on me :)

Thanks in advance

adamwlarson avatar May 02 '12 20:05 adamwlarson

use connect instead of bindSync for the push.

markcode avatar Sep 25 '12 00:09 markcode

@maddogawl was your problem solved? If yes please close this case.

rubentlz avatar Jan 26 '13 16:01 rubentlz