0

Sorry if I ask silly question, I am working with the example below regarding on nodejs long polling. http://www.stoimen.com/blog/2010/12/02/diving-into-node-js-a-long-polling-example/

I understand most of them, but just one thing I am not quite able to understand although I have done thousands of searching.

fs.stat('filepath', function(err, stats) {
    // if the file is changed
    if (stats.mtime.getTime() > request.socket._idleStart.getTime()) {
        // read it
        fs.readFile('filepath', 'utf8', function(err, data) {
            // return the contents
            response.writeHead(200, {
                'Content-Type'   : 'text/plain',
                'Access-Control-Allow-Origin' : '*'
            });

            // return response
            response.write(data, 'utf8');
            response.end();

            // return
            return false;
        });
    }
});

The part "request.socket._idleStart", what is the meaning of the parameter _idleStart? Actually, I try to print out the whole request object and got the following parameters.

_readableState: 
   { highWaterMark: 16384,
     buffer: [],
     length: 0,
     pipes: null,
     pipesCount: 0,
     flowing: false,
     ended: false,
     endEmitted: false,
     reading: false,
     calledRead: false,
     sync: true,
     needReadable: false,
     emittedReadable: false,
     readableListening: false,
     objectMode: false,
     defaultEncoding: 'utf8',
     ranOut: false,
     awaitDrain: 0,
     readingMore: false,
     decoder: null,
     encoding: null },
  readable: true,
  domain: null,
  _events: {},
  _maxListeners: 10,
  socket: 
....
...
...

I am wondering if there is any documentations describing these parameters, thanks for all for the help!

1 Answer 1

1

those parameters with _ underscore, are used for maintaining the state of socket, its not meant for working with them. there are functions that are more reliable than those.

from node.js documentation

readable._read

Note: This function should NOT be called directly. It should be implemented by child classes, and called by the internal Readable class methods only.

All Readable stream implementations must provide a _read method to fetch data from the underlying resource.

This method is prefixed with an underscore because it is internal to the class that defines it, and should not be called directly by user programs. However, you are expected to override this method in your own extension classes.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer, if I understand correctly, those values have functions to retrieve them instead of letting me to call directly, right?
@user2499325 yes correct, imagine "private" variables you can never access except from a public function. Note, that functions like .read() depend on these variables, for appropriate behavior, changing values directly might result to something unexpected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.