How to access the data of a feed?

advertisements

I'm working with this library: mTwitter

My problem is, when I want to use the streaming function:

twit.stream.raw(
  'GET',
  'https://stream.twitter.com/1.1/statuses/sample.json',
  {delimited: 'length'},
    process.stdout
);

I don't know how to access the JSON that generates process.stdout.


You could use a writable stream, from stream.Writable.

var stream = require('stream');
var fs = require('fs');

// This is where we will be "writing" the twitter stream to.
var writable = new stream.Writable();

// We listen for when the `pipe` method is called. I'm willing to bet that
// `twit.stream.raw` pipes to stream to a writable stream.
writable.on('pipe', function (src) {

  // We listen for when data is being read.
  src.on('data', function (data) {
    // Everything should be in the `data` parameter.
  });

  // Wrap things up when the reader is done.
  src.on('end', function () {
    // Do stuff when the stream ends.
  });

});

twit.stream.raw(
  'GET',
  'https://stream.twitter.com/1.1/statuses/sample.json',
  {delimited: 'length'},

  // Instead of `process.stdout`, you would pipe to `writable`.
  writable
);