ttezel / twit

Twitter API Client for node (REST & Streaming API)
4.31k stars 568 forks source link

Calling stream.stop() doesn't stop stream #478

Open ctcuff opened 6 years ago

ctcuff commented 6 years ago

I have the following code:

const Twit = require('twit');
const config = require('./config');
const T = new Twit(config);

let stream;
let hasStarted = false;

function startStream() {
  let input = document.getElementById('input-keyword');
  let body = document.getElementById('tweet-body');
  let keyword = input.value;

  if (keyword === '') {
    return;
  }
  console.log(`Stream started -> ${keyword}`);
  input.value = '';

  if (!hasStarted) {
    stream = T.stream('statuses/filter', {track: keyword});
  }

  stream.on('tweet', tweet => {
    console.log(`@${tweet.user.screen_name} => [${tweet.text}]`);
    body.innerHTML += `<code>@${tweet.user.screen_name} => [${tweet.text}]</code><br><br>`
  });

  hasStarted = true; 
}

function stopStream() {
  if (hasStarted) {
    console.log('Stream stopped');
    stream.stop();
  }
  hasStarted = false; 
}

The problem is that no matter how many times i call stopStream(), the stream continues. I'm not sure if this is just a problem with my set up but I honestly don't see what's wrong here

mef commented 5 years ago

In the portion of code shared above, stopStream is never called.

stream.stop works correctly in the code I'm using on my end, pretty similar to the one shared above.

dandv commented 5 years ago

This library seems no longer maintained.

Here's how to stop streams in twitter-lite.

See also how to switch streams.

edsu commented 5 years ago

I think I'm seeing the same behavior where repeated calls to stream.stop() are no longer working. I think it was working in the past, perhaps with an older version of twit?