Note that there are some explanatory texts on larger screens.

plurals
  1. POnode.js callback being called multiple times
    primarykey
    data
    text
    <p>I just started playing with node.js and the zeromq bindings today and am seeing some behavior that I don't quite understand.</p> <p>Here's my sample code which calls to another ruby based zeromq REP process on port 9000 on the same machine:</p> <pre><code>var zeromq = require("zmq"); var http = require('http'); var counter = 0; var availabilityResponse = function(counter, data, request, response) { var str = data.toString('utf-8'); console.log(str); response.writeHead(200, {'Content-Type': 'text/plain'}); response.write(str); response.end(); } var zmqSocket = zeromq.createSocket('request'); zmqSocket.connect("tcp://127.0.0.1:9000"); var server = http.createServer() server.on('request', function(request, response) { counter += 1; zmqSocket.send("Hola! - This is call # " + counter); zmqSocket.on('message', function(data) { availabilityResponse(counter, data, request, response); }); }); server.listen(8888); console.log("Starting HTTP server on port 8888"); </code></pre> <p>I'm hitting the node server using my browser and waiting for a response. What is strange is that the output I get in the console is coming across in an additive manner which I'm not grokking:</p> <pre><code>Request received: Hola! - This is call # 1 - Now going to sleep for 1.0 Request received: Hola! - This is call # 2 - Now going to sleep for 3.0 Request received: Hola! - This is call # 2 - Now going to sleep for 3.0 Request received: Hola! - This is call # 3 - Now going to sleep for 3.0 Request received: Hola! - This is call # 3 - Now going to sleep for 3.0 Request received: Hola! - This is call # 3 - Now going to sleep for 3.0 Request received: Hola! - This is call # 4 - Now going to sleep for 2.0 Request received: Hola! - This is call # 4 - Now going to sleep for 2.0 Request received: Hola! - This is call # 4 - Now going to sleep for 2.0 Request received: Hola! - This is call # 4 - Now going to sleep for 2.0 </code></pre> <p>I tried moving the zmqSocket.on() call outside of the http request block which removes the additive behavior but it also doesn't then include the request/response objects that I need. </p> <p>The point of this exercise is to have the browser wait until the response comes back from the long running zeromq server process on port 9000 (currently just a faked up sleep response). </p> <p>I'm pretty sure I'm just misunderstanding the callback here but can't find much in the way of documentation on this scenario.</p> <p>Thanks for any help.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload