0

I connect to twitter streaming API using Play 2.2 WS API in the code example below. I'm stuck trying to figure out how to disconnect from stream once it's established. Is there any proper way to do that rather than stopping application? Any help will be appreciated.

def watchTweets(keywords : String) = Action { implicit request =>
Logger.debug(s"watchTweets invoked with: $keywords")
val (tweetsOut, tweetChanel) = Concurrent.broadcast[JsValue]
WS.url(s"https://stream.twitter.com/1.1/statuses/filter.json?track=" + URLEncoder.encode(keywords, "UTF-8"))
  .sign(OAuthCalculator(Twitter.KEY, Twitter.sessionTokenPair.get))
  .postAndRetrieveStream("")(headers => Iteratee.foreach[Array[Byte]] { ba =>
  val msg = new String(ba, "UTF-8")
  Logger.debug(s"received message: $msg")
  val tweet = Json.parse(msg)
  tweetChanel.push(tweet)
}).flatMap(_.run)

Ok.chunked(tweetsOut &> Comet(callback = "parent.cometMessage")) }

1 Answer 1

1

This is a known issue... the answer is that you close the connection, but the problem is, the enumerator won't notice that the connection is closed until it tries to feed a tweet to the client, and that won't happen until it receives another tweet from Twitter, which might take a long time to happen.

This is of course a problem when doing Twitter streaming because Twitter will only let a user create one stream at a time, so that effectively means that you can't do a second stream until the first receives some data.

Unfortunately we don't have a work around as yet, but we are looking at introducing a new lower level streaming API into Play around the 2.4 time line, which will certainly allow this.

3
  • So how is it handled in 2.4 ?
    – sam
    Nov 2, 2015 at 11:37
  • 1
    Not solved in 2.4. But in 2.5 (in progress), we've switched to Akka streams, which provides a way to asynchronously send a cancel upstream. Nov 18, 2015 at 4:11
  • I noticed that this time the WC class extends the Closeable Interface :) , so it should be easy to cose that indeed
    – sam
    Nov 18, 2015 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.