0

I'm using the streaming features of the Twitter library. When a user registers, it begins streaming their tweets:

class UserStream
  def begin_stream()
    Thread.new do
      # Setup the client
      @client = Twitter::Streaming::Client.new do |config|
      config.consumer_key        = ConsumerKey
      config.consumer_secret     = ConsumerSecret
      config.access_token        = @access_token
      config.access_token_secret = @access_token_secret
    end

    @client.user do |object|
      puts object
      yield object
    end
  end
end

The UserStream object is then added to an array, to keep the stream active. The begin_stream function is 'yielded' by another function, to receive the events:

user_stream = UserStream.new(user_id, access_token, access_token_secret)
@user_streams << user_stream

Thread.new do
  user_stream.begin_stream do |message|
    # Respond to event
  end
end

But when a UserStream is removed from the array later on, the events keep firing. My intention upon removing it from the array is to make it nil due to it having no references, and to stop the stream. The Twitter Streaming Client doesn't appear to have a way to stop the stream.

EDIT: Code for removing the stream:

def remove_stream(user_id)
  index = @user_streams.index do |user_stream|
    user_stream.user_id == user_id
  end

  if index
    @user_streams.delete_at(index)
  end
end
Andrew
  • 7,693
  • 11
  • 43
  • 81
  • Why are you threading this? (twice no less) Also I do not see you removing the stream either since this seems to be the issue I would recommend posting that code as well. – engineersmnky Jul 07 '16 at 17:57
  • @engineersmnky Threading because otherwise the code following it doesn't run, it's waiting to yield. It's possible I could remove one of the threads. I've edited with the code for removing the stream. – Andrew Jul 07 '16 at 20:35

0 Answers0