I want to pull posts from a users Facebook wall.
The following code snippet works, but it never terminates:
var https = require('https');
facebookWall = function(user) {
var options = {
host: 'graph.facebook.com',
port: 443,
path: '/me/home?access_token=' + user.facebook_token + '&since=' + encodeURIComponent(user.facebook_timestamp),
method: 'GET',
headers: {
'Connection':'keep-alive'
}
};
var req = https.request(options)
.on('response', function(response) {
var body = '';
response.on('data', function(data) {
body += data;
try {
var wallPosts = JSON.parse(body);
console.log("user " + user.id + " has " + wallPosts.data.length + " new items on their wall");
}
catch (e) {
//console.log("waiting for more data chunks...");
}
})
});
req.end();
req.on('error', function(e) {
console.error(e);
});
}
I think it is caused by the 'Connection':'keep-alive' header. When I replace it with 'Connection':'close' the script will terminate when all data has been retrieved from facebook.
I’m hoping to be able to use the keep-alive header to prevent having to create a new SSL connection for each request. I have thousands of requests and with the keep-alive header, it completes in just a few seconds, as opposed to a few minutes without the keep-alive header.
Does anyone know how to accomplish this? I’m fairly new to Node.JS, so if I’m missing something obvious, I apologize.
It’s because keep-alive is not yet implemented for https/tls/ssl
in node 4.x and I believe for 6.x too. That’s why in node websocket-server
it doesn’t work as well, see https://github.com/nephics/node-websocket-server/commit/3a732bff6aabe694834d87086a7718be7c0ce138