4

I'm new to node.js so please excuse the simple question. I'm writing something standard: a rest API that goes to twitter using ntwitter and saves stuff on mysql. What I'm having trouble is disconnecting and connecting back to twitter using ntwitter.

I checked a somewhat similar question to mine but that one is still unanswered: Restarting nodejs ntwitter twitter stream with different track keywords

The question is two fold:

1) How to check if the twit.stream is alive
2) How to kill it/manually disconnect from twitter

I'm using destroy but that does not seem to work. The relevant part of the code is:

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

I think you have a setup somewhere in your code like this:

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

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

  twit.currentTwitStream = stream;

This is only a suggestion. Perhaps you have a central configuration object which would be a better place for the current stream object. With currentTwitStream you can do this:

  1. currentTwitStream.readable is false after an error or close.
  2. currentTwitStream.destroy() destroys the stream.
  • thanks, this worked like a charm :-) – icaro Oct 4 '12 at 3:52
  • I know this an old post, but could you provide a more clear example on how you destroy the stream. – FiringSquadWitness Oct 21 '14 at 3:34
  • I am sorry, but no, because I don't have the time to test carefully. – nalply Oct 21 '14 at 7:54
1

I was facing this problem too. What eventually worked for me was stream.stop().

.destroy(), .disconnect() etc brought up a variety of errors such as

.destroy is not defined.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.