Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js https.request with keep-alive header

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.

like image 788
Teetotum Avatar asked Jun 13 '11 22:06

Teetotum


People also ask

How do I keep a socket connection alive in node JS?

setKeepAlive() method enables/disables TCP keep alive. This is done at the TCP level in the OS, so it is enabled by the node. js socket library, but the keep-alive functionality is actually implemented in the TCP stack in the host OS.

How do you pass a header request in node JS?

We will use request. setHeader() to set header of our request. The header tells the server details about the request such as what type of data the client, user, or request wants in the response. Type can be html , text , JSON , cookies or others.

What is setHeader in node JS?

setHeader(name, value) (Added in v0. 4.0) method is an inbuilt application programming interface of the 'http' module which sets a single header value for implicit headers. If this header already exists in the to-be-sent headers, its value will be replaced.

How do I use HTTP request in node JS?

Setup a new project: To create a new project, enter the following command in your terminal. Project Structure: It will look like the following. Approach 1: In this approach we will send request to getting a resource using AXIOS library. Axios is a promise base HTTP client for NodeJS.


1 Answers

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

like image 146
Mikhail.Mamaev Avatar answered Sep 24 '22 00:09

Mikhail.Mamaev