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