Seems there's a lot of discussion about how to do this but I haven't found any concrete examples. My node application is accessing a RESTful API that is just going to pass through to a database. my initial solution is this
function getRecord() {
var req = https.request(options, function(res){
res.on('data', function(data) {
var record = JSON.parse(data.toString('utf-8'));
//do some database things then do it all over
getRecord();
});
});
req.write(queryString);
req.end();
}
This accomplishes what I want. I'm going to keep getting new records from the API, but I'm not sure if this is the best approach to doing what I want.
One (especially favorable) alternative is to switch to a bus architecture with a message server such as RabbitMQ.
You would need to either control the API or have an API that you can register callbacks with.
Long polling is a strategy that can degrade performance and bloat logs quite quickly. Event driven methods, like I've listed above, are much preferred if its a possibility. And, unlike long polling, event driven strategies don't introduce any latency. So if latency is a concern, the more frequent you poll, the more it will degrade performance and bloat logs.