I have a node express/ socket application where my express server makes several API calls with node-rest-client looping through elements in var jobs and when each finishes, it sends the data via socket io to the client. However, every now and then i get a socket hang error after about 1000 or so API calls.
events.js:182
throw er; // Unhandled 'error' event
^
Error: socket hang up
at createHangUpError (_http_client.js:345:15)
at Socket.socketOnEnd (_http_client.js:437:23)
at emitNone (events.js:110:20)
at Socket.emit (events.js:207:7)
at endReadableNT (_stream_readable.js:1059:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
How do you error handle these errors? Or perhaps my initial attempt to code this function is poor, and in which case, any suggestion on how to make multiple API calls and emit the results to all sockets connect? (Requirement, the only way i can get the information is through making these API calls).
Server:
setInterval(function(){
var jobs = ['J1', 'J2', 'J3', 'J4'];
var full_data = {};
for(var i = 0; i < jobs.length; i++){
client.get("MY URL", function (data, response) {
io.sockets.emit('progressbar', data);
});
}
console.log(full_data);
}, 5000)
Where, 'progressbar' is the client function listening for data.