twitter4s icon indicating copy to clipboard operation
twitter4s copied to clipboard

Not possible to get callback on stream completion

Open nilsga opened this issue 7 years ago • 5 comments

In the event of an error that causes the stream to close, it does not seem to be a way to be notified about this, e.g. to shut the application down. The actual Future of the stream is "hidden" inside the Future[TwitterStream] that the streaming client exposes, so the only completion one is notified about is if the initial connect succeeds or fails. Neither does it seem to be any control messages to listen to in this case.

nilsga avatar Oct 16 '17 12:10 nilsga

What I would like to do is something like:

val streamFuture = client.filterStatuses(tracks = trackTerms)(processTweet)
streamFuture.onComplete(_ => {
  println("Done")
  System.exit(0)
})

But this does not work, since the future that is returned is the connection future, and not the stream future.

nilsga avatar Oct 16 '17 12:10 nilsga

Hi @nilsga, according to the specification of the Twitter Streaming API, once the streaming connection is open, it will never be closed unless requested from the consumer of the API: the stream is potentially infinite.

What you can do is closing or replacing an existing stream on demand: have a look at this section of the documentation that shows how to do that.

If this doesn't help you achieve what you are trying to do, please let us know!

Cheers, D.

DanielaSfregola avatar Oct 16 '17 14:10 DanielaSfregola

That's true, but there are cases where the stream can fail, not related to the twitter api. For instance if the connection times out from the akka stream end, or there are errors processing the tweet itself. In these cases, it would be nice to be able to just shut down, instead of running with a halted flow.

nilsga avatar Oct 16 '17 14:10 nilsga

Interesting: I didn't think of that! At present, you cannot do it.

What about having the opportunity of specifying an "ErrorHandler" to specify what the stream should behave depending on the exception thrown?

DanielaSfregola avatar Oct 16 '17 14:10 DanielaSfregola

Maybe. The reason I added this in addition to https://github.com/DanielaSfregola/twitter4s/issues/142 was that I had a very low volume stream, and akka streams reached an idle timeout. Not sure how these low level "protocol" exceptions would propagate to the client.

nilsga avatar Oct 16 '17 14:10 nilsga