Clean way to break out of a streaming connection?

102 views
Skip to first unread message

Ken Anderson

unread,
May 13, 2014, 7:13:46 PM5/13/14
to typh...@googlegroups.com
I'm a new user of Typhoeus and I'm having a lot of fun. I'm using Typhoeus to connect to the Twitter Streaming API and it works great.

I'm looking for a way to cleanly terminate a streaming connection and I've scanned the documentation and searched the web but can't find anything on this particular topic. I've got an on_body callback defined that looks something like this:

request.on_body do |chunk|
  # add chunk to buffer
  # while messages exist in buffer
  #   add next message to queue for later processing
  # end
  # !!! check for request to shutdown ; if shutdown requested, cleaning terminate streaming request and shutdown
end

I have a mechanism for contacting my program and requesting a shutdown, what I need help with is how do I tell Typhoeus that I'm done with the streaming connection. I don't want any more calls to on_body and instead I'd like the request to finish and the on_complete handler to fire.

Is this possible?

Thanks!

Ken

Ken Anderson

unread,
May 15, 2014, 8:28:39 PM5/15/14
to typh...@googlegroups.com
As an update on my own question, I found that this line of code will do the job. It's probably overkill but works:

Thread.current.exit unless @continue

@continue is a boolean that starts out true and gets set to false when we have received a request to shutdown. Since this line is executed within the context of the on_body callback, Thread.current is set to the thread that Typhoeus is using to monitor data coming from the Twitter Streaming API.

Are there better ways to do this?

On Tuesday, May 13, 2014 5:13:46 PM UTC-6, Ken Anderson wrote:
I'm a new user of Typhoeus and I'm having a lot of fun. I'm using Typhoeus to connect to the Twitter Streaming API and it works great.

I'm looking for a way to cleanly terminate a streaming connection and I've scanned the documentation and searched the web but can't find anything on this particular topic. I've got an on_body callback defined that looks something like this:

request.on_body do |chunk|
  # add chunk to buffer
  # while messages exist in buffer
  #   add next message to queue for later processing
  # end
  # !!! check for request to shutdown ; if shutdown requested, cleanly terminate streaming request and shutdown
end

I have a mechanism for contacting my program and requesting a shutdown, what I need help with is how do I tell Typhoeus that I'm done with the streaming connection. I don't want any more calls to on_body and instead I'd like the request to finish and the on_complete handler to fire.

me

unread,
May 16, 2014, 2:46:17 AM5/16/14
to typh...@googlegroups.com
Hello Ken,

the solution you found is rather drastic. There is no way to do it though atm although it shouldn't be too hard. There was a similar question on stackoverflow recently: http://stackoverflow.com/questions/23383959/how-to-abort-current-request-in-on-body-callback-using-typhoeus. Could you create a github issue? Maybe you even want to take a stab at it? :)
Reply all
Reply to author
Forward
0 new messages