Restart nodejs ntwitter twitter stream with various track keywords

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 am new to nodejs. What is the best way to stop this and start it again with a different set of keywords.

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

+3
source share
1 answer

Iโ€™m completely a noob, so maybe itโ€™s not very good, and he wastes resources, but I still donโ€™t know how to check it, so I will drop it here and hope that some of the masters would tell us OK or is it wrong, and most importantly, why ?.

The way is to put a tw.stream call inside a function and call that function with an array of words that you want to track. It will start tracking new words and stop tracking deleted 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 that everything is in order, because this is the only way I found.

0
source

Source: https://habr.com/ru/post/1436745/


All Articles