1

//Totally new to node.js

I'm using ntwitter to listen on twitter statuses. How can I stop listening after X callback were called?

 var twittsCounter = 0;
 twit.stream('statuses/filter', {track:['cool','awesome','fuck yeah!']}, function(stream) {
  stream.on('data', function (tweet) {
    twittsCounter += 1;
    console.log(twittsCounter + ":" + tweet.text);
    if (twittsCounter > 100){
      //stop listening? 
    }

  });
Yossale
  • 14,165
  • 22
  • 82
  • 109

1 Answers1

3

Factor your event handler into a separate function. Then use

stream.on('data', myDataHandler)

... and in myDataHandler just use stream.removeListener('data', myDataHandler).

stream is "just" an EventEmitter, here are the docs: http://nodejs.org/api/events.html

Nitzan Shaked
  • 13,460
  • 5
  • 45
  • 54