0

I'm running a streaming connection with twitter gem inside a thread. I have been disconnecting by simply killing the thread and opening a new one. I have an overlap between the old and new threads so I don't miss anything (hopefully).

But if I kill a few quickly, I receive a Exceeded connection limit for user error from the API. This is because twitter only allows 2 connections at once. It seems like the connection is still open...

Is there a way to disconnect using the twitter gem? I want to disconnect the streams before I kill the thread so I know how many are open.

My code looks something like this:

thred = Thread.new do
  client = ::Twitter::Streaming::Client.new(..config..)
  client.filter(follow: '1,2,44') do |tweet|
    // do stuff
  end
end

And I am killing them like so:

thred.kill

But I want to run something like:

client.disconnect

And then tidy up dead threads or something...

Rimian
  • 36,864
  • 16
  • 117
  • 117
  • 3
    According to the docs it has a `close` https://www.rubydoc.info/gems/twitter/Twitter/Streaming/Client#close-instance_method – Tin Nguyen Mar 25 '20 at 07:36

1 Answers1

0

Thanks @tin-nguyen for directing me to the docs.

My solution goes something like this:

thredbo = lambda do |client, str|
  Thread.current.thread_variable_set(:client, client)

  follows = '1,2,3,4' # twitter ids. This is where I query the DB for accounts.

  client.filter(follow: follows) do |tweet|
    puts [str, 'client response'].join(' ')
  end

  puts [str, 'connection closed'].join(' ')
end

config = {} # your credentials

main = Thread.new(
  ::Twitter::Streaming::Client.new(config),
  'main',
  &thredbo
)

# close
main.thread_variable_get(:client).close

After the connection is closed the thread is finished.

Rimian
  • 36,864
  • 16
  • 117
  • 117