user3705887
user3705887

Reputation: 57

Node.js and zeroMQ request and response

I have built two Javascript-files, where I implement a responder and requester within zeroMQ and node.js. Once i start them, the first one sends all the requests, but the second code snippet doesn't receive them. I bound them to the same address and port. Is that right?

Here is the requester:

// Erstelle zeroMQ-Socket
var zmq = require('zmq')
var requester = zmq.socket('req');

// Request-Client verbindet sich mit TCP-Socket
requester.connect('tcp://localhost:5000');
var replyNbr = 0;

// Erhalte Nachricht auf Request
requester.on('message', function(msg) {
    console.log('got reply', replyNbr, msg.toString());
    replyNbr += 1;
});

// Sende request an Server
for (var i = 0; i < 10; ++i) {
requester.send("Hello");
console.log("Sende Request" + (i+1));
}

And here is the responder:

// Erstelle Socket-Verbindung
var zmq = require('zmq')
var responder = zmq.socket('rep');

// Verbinde zum TCP-Socket
responder.connect('tcp://localhost:5000');
console.log('Warte auf Request...');

// Warten bis Request ankommt
responder.on('message', function(msg) {
    console.log('received request:', msg.toString());

    // Warte eine Sekunde und schicke Antwort
    setTimeout(function() {
        responder.send("World");
    }, 1000);
});

Edit to include info posted in an answer below:

I now turned over to the req_rep.js example of the zmq node_modules directory. There is this example:

/*
 *
 * One requester two responders (round robin)
 *
 */

var cluster = require('cluster')
    , zeromq = require('zmq')
    , port = 'tcp://127.0.0.1:12345';

if (cluster.isMaster) {
    //Fork servers.
    for (var i = 0; i < 2; i++) {
        cluster.fork();
    }

    cluster.on('death', function(worker) {
        console.log('worker ' + worker.pid + ' died');
    });

    //requester = client

    var socket = zeromq.socket('req');

    socket.identity = 'client' + process.pid;

    socket.bind(port, function(err) {
        if (err) throw err;
        console.log('bound!');

        setInterval(function() {
            var value = Math.floor(Math.random()*100);

            console.log(socket.identity + ': asking ' + value);
            socket.send(value);
        }, 100);


        socket.on('message', function(data) {
            console.log(socket.identity + ': answer data ' + data);
        });
    });
} else {
    //responder = server

    var socket = zeromq.socket('rep');

    socket.identity = 'server' + process.pid;

    socket.connect(port);
    console.log('connected!');

    socket.on('message', function(data) {
        console.log(socket.identity + ': received ' + data.toString());
        socket.send(data * 2);
    });
}

That works fine, but when I try to get the requester in one file and the responder in one file, nothing works anymore. Could anybody help me there?

File 1:

// Requester (bildet Webserver ab)

// zmq und Adresse einbinden
var zeromq = require('zmq')
    , port = 'tcp://127.0.0-1:12345';

// socket erstellen und an Adresse binden

var socket = zeromq.socket('req');

socket.identity = 'client' + process.pid;

socket.bind(port);
console.log('An Port gebunden');

setInterval(function() {
    var value = Math.floor(Math.random()*100);

    console.log(socket.identity + ': Anfrage ' + value);
    socket.send(value);
}, 2000);

socket.on('message', function(data) {
console.log(socket.identity + ': Antwort von Responder ' + data);
});

File 2:

// Erstelle Socket-Verbindung
var zeromq = require('zmq')
    , port = 'tcp://127.0.0.1:12345';

var socket = zeromq.socket('rep');

socket.identity = 'server' + process.pid;

// Responder mit Adresse verbinden
socket.connect(port);
console.log('connected!');

// Auf Anfrage des Client warten
socket.on('message', function(data) {
    console.log(socket.identity + ': Erhalten ' + data.toString());
    socket.send(data * 2);
});

EDIT: FOUND THE SOULTION! IN FILE 1 the port address is in a wrong format! Now everything works!

Upvotes: 3

Views: 2838

Answers (1)

Jason
Jason

Reputation: 13766

You've used connect on both sides of the communication, you need one side to connect and the other side to bind to the address:port. Typically you will choose the side that will act as the "server" or the side that is more reliable to bind on, and you will connect with the "client", or the side that will come and go. This looks like just a basic example, so it doesn't really matter which side you pick to be which.

For the sake of an example, I'm going to assume you want your REP socket to bind:

// Erstelle Socket-Verbindung
var zmq = require('zmq')
var responder = zmq.socket('rep');

// Verbinde zum TCP-Socket
responder.bindSync('tcp://localhost:5000');
console.log('Warte auf Request...');

... you'll see that I used bindSync, that just removes the asynchronous concerns from your code, whether you want to use bind or bindSync in production depends on your situation.

Upvotes: 1

Related Questions