Roland Arias
Roland Arias

Reputation: 78

How do I access the data from a stream?

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.

Upvotes: 0

Views: 361

Answers (2)

Sal Rahman
Sal Rahman

Reputation: 4746

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
);

Upvotes: 1

slebetman
slebetman

Reputation: 113926

I'm not sure if you really understand what the word streaming means here. In node.js a stream is basically a file descriptor. The example uses process.stdout but a tcp socket is also a stream, an open file is also a stream and a pipe is also a stream.

So a streaming function is designed to pass the received data directly to a stream without needing you to manually copy the data from source to destination. Obviously this means you don't get access to the data. Think of streaming like pipes on unix shells. That piece of code is basically doing this:

twit_get | cat

In fact, in node, you can create virtual streams in pure js. So it is possible to get the data - you just have to implement a stream. Look at the node documentation of the stream API: http://nodejs.org/api/stream.html

Upvotes: 0

Related Questions