How to stop and restart twitter stream using ntwitter

I am new to node.js, so please excuse a simple question. I am writing something standard: a leisure API that goes to twitter using ntwitter and saves stuff in mysql. I am having problems disconnecting and connecting to twitter using ntwitter.

I checked a somewhat similar question to mine, but he still did not answer: Restarting the ntwitter twitter stream node with various track keywords

Question two times:

1) How to check if twit.stream alive
2) How to kill him / disable manually from twitter

I use destroy , but this does not work. Relevant piece of code:

 if (values[0] == 'newStatusAdded') { console.log('Need to stop twitter listener'); //check if twitter stream is running if (twit.stream) { twit.stream.destroy; debugger; console.log('Stopped twitter listener'); } 

thanks....

+4
source share
2 answers

I think you have a setting somewhere in your code, for example:

 twit.stream('statuses/sample', function(stream) { stream.on('data', function (data) { console.log(data); }); }); 

Save the stream variable from the callback, for example, after stream.on() :

  twit.currentTwitStream = stream; 

This is just an assumption. Perhaps you have a central configuration object that will be the best place for the current thread object. With currentTwitStream you can do this:

  • currentTwitStream.readable is false after an error or close.
  • currentTwitStream.destroy() destroys the stream.
+4
source

I also ran into this problem. What ultimately worked for me was stream.stop() .

.destroy() , .disconnect() , etc. caused a lot of mistakes like

.destroy is undefined.

+1
source

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


All Articles