0

I have the following situation, where the already sent headers problem happens, when sending multiple request from the server to the client via AJAX: app structure

It is something I expected since I opted to go with AJAX, instead of sockets. Is there is other way around to exchange the data between the server and the client, like using browserify to translate an emitter script for the client? I suppose that I can't escape the sockets, so I will take advice about simpler library, as sockets.io seems too complex for such a small operation. //------------------------- Update: Here is the node.js code as requested.

  var maxRunning = 1;
  var test_de_rf = ['rennen','ausgehen'];

function callHandler(word, cb) {
    console.log("word is - " + word);
    gender.gender_function_rf( word , function (result_rf) {
      console.log(result_rf);
        res.send(result_rf);// Here I send data back to the ajax call
        setTimeout(function() { cb(null);
        }, 3000);
    });
}
async.eachLimit(test_de_rf, maxRunning, function(item, done) {
    callHandler(item, function(err) {
        if (err) throw new Error(err);
        done();
    });
}, function(err) {
    if (err) throw new Error(err);
    console.log('done');
});
Dejan Toteff
  • 2,075
  • 3
  • 21
  • 30
  • Can we see the node.js code that is giving you the problems for your ajax request? – bluetoft Mar 08 '15 at 21:18
  • I'd hardly say socket.io is complex. In fact, it simplifies somethings that you have to do yourself with a webSocket. If you want a dedicated connection by which you can send messages to the client at any time, then a webSocket at its core is what you want from a browser to a server and socket.io is a higher level implementation on top of webSockets (which gives you a bunch of useful features for free like automatic reconnection). – jfriend00 Mar 08 '15 at 23:35

1 Answers1

0

res.send() sends and finishes an http response. You can only call it once per request because the request is finished and done after calling that. It is a fairly high level way of sending a response (does it all at once in one call).

If you wanted to have several different functions contributing to a response, you could use the lower level functions on the http object such as res.setHeader(), res.writeHead(), res.write() (which you can call multiple times) and res.end() (which indicates the end of the response).

You can use the standard webSocket API in the browser and get webSocket module for server-side support or you can use socket.io which offers both client and server support and a number of higher level functions (such as automatic reconnect, automatic failover to http polling if webSockets are not supported, etc...).


All that said, if what you really want is the ability to just send some data from server to client whenever you want, then a webSocket is really the better way to go. This is a persistent connection, is supported by all modern browsers and allows the server to send data unsolicited to the client at any time. I'd hardly say socket.io is complex. The doc isn't particularly great at explaining things (not uncommon in the open source world as the node.js doc isn't particularly great either). But, I've always been able to figure advanced things out by just looking at a few runtime data structures in the debugger and/or looking at the source code.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Thanks, mate! After you confirmed that websocket was the answer, I quickly manage to work it out thanks to the code from http://stackoverflow.com/questions/9914816/what-is-an-example-of-the-simplest-possible-socket-io-example – Dejan Toteff Mar 09 '15 at 13:49