3
var twitter = require('ntwitter');
// Configure twitter

var keywords = ['hello', 'world'];

twit.stream('statuses/filter', {'track':keywords.join(',')}, function(stream) {
  stream.on('data', function (data) {
    console.log(data);
  });

  stream.on('end', function (response) {
    console.log("\n====================================================");
    console.log("DESTROYING");
    console.log("====================================================\n");
  });

  setTimeout(function(){
    stream.destroy();
  }, 60000);
});

I'm new to nodejs. What is the best way to stop this and start it again with but a different set of keywords.

I can destroy() the stream and then create a new one. But is there anyway I can just change the track keywords without disconnecting?

Mooktakim Ahmed
  • 1,021
  • 1
  • 10
  • 17

1 Answers1

0

I'm quite noob, so maybe this way it's not a good one and it's wasting resources, but I still don't know how to check it, so I will drop it here and hope that someone skilled told us if it's OK or it's wrong, and the most important, why?.

The way is to put the tw.stream call, inside a function and call that function with the array of words you want to track. It will start tracking new words, and stop tracking removed words:

// Array to store the tracked words
var TwitWords = [];

// Tracker function
function TrackWords(array){
  tw.stream('statuses/filter',{track:array},function(stream){
    stream.on('data',function(data){
      console.log(data.text);
    });
  });
}

// Add word
function AddTwitWord(word){
  if(TwitWords.indexOf(word)==-1){
    TwitWords.push(word);
    TrackWords(TwitWords);
  }
}

// Remove word
function RemoveTwitWord(word){
  if(TwitWords.indexOf(word)!=-1){
    TwitWords.splice(TwitWords.indexOf(word),1);
    TrackWords(TwitWords);
  }
}

I hope it's ok, because it's the only way I found.

Riwels
  • 786
  • 10
  • 21
  • 1
    You don't seem to be destroying the currently running stream. When you start a new one, while there is one running, does it close the old one? – Mooktakim Ahmed Jan 30 '13 at 16:28
  • As I said, I still don't know how to debug it, so I can't confirm it. Looking how it works, it looks like it's doing the work so fine. If you are tracking coke and pepsi and you call TrackWords with fanta and kas, it stops tracking coke and pepsi. It looks fine, but maybe it's wasting memory or something. – Riwels Jan 30 '13 at 20:30
  • twitter allows only one stream per user account, So when you start a new stream without destroying the previous one...it will automatically be closed down. But its still better to destroy it yourself. – Abhidev Aug 18 '14 at 03:54
  • Could you expand on how I can destroy a stream Abhidev? – FiringSquadWitness Oct 21 '14 at 03:30