longtime listener, first time caller.
I need to use Node as a client to connect to a realtime data stream that uses long polling and do stuff with each item as the data is received.
I've found plenty of info on using Node as a long polling server, but not as the client.
I know how to use the "Request" module to load a URL, but my problem is the only callback I know of is the "oncomplete" callback, which only fires after the connection is closed. It doesn't allow me to access the data being received in real time while the connection is held open. I only get to use it when the connection terminates.
Is there a way for Node to open an HTTP connection to a remote server, then fire events whenever data is received?
Or I suppose another question is... is there a way to access whatever buffer all that data is going into while the HTTP connection is in progress?
Thanks!!!
Does the example in http.request() work?
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.end();
Don't use the request module. If you use what's built into node, it is quite simple.
http://nodejs.org/api/http.html#http_event_data
response.on('data', function (chunk) {
// chunk is the data just received
})
The request module itself does have a similar method, but I don't know what it is off the top of my head.