2

I am using Play 2.6 and The twitter streaming API. Below is how I connect to twitter using the ws library's stream() method.

The problem is that, the steam always stops after exactly 2 minutes. I tried different topics and the behavior is pretty consistent. It seems there is a setting but I could not find where.

I am not sure it's on the play side or twitter side. Any help is greatly appreciated.

  ws.url("https://stream.twitter.com/1.1/statuses/filter.json")
          .sign(OAuthCalculator(ConsumerKey(credentials._1, credentials._2), RequestToken(credentials._3, credentials._4)))
          .withQueryStringParameters("track" -> topic)
          .withMethod("POST")
          .stream()
          .map {
            response => response.bodyAsSource.map(t=> {t.utf8String})
          }
Haijin
  • 2,561
  • 2
  • 17
  • 30
  • I believe this is the duplication of this: https://stackoverflow.com/questions/39054030/playframework-and-twitter-streaming-api – o-0 Jan 31 '18 at 07:17
  • In fact, I think I probably followed that post in the code. But the strange behavior is not mentioned in that post. Maybe it's not noticed , or it's a new version thing. – Haijin Jan 31 '18 at 07:38

1 Answers1

3

Play WS has default request timeout which is exactly 2 minutes by default. Here is link to the docs: https://www.playframework.com/documentation/2.6.x/ScalaWS#Configuring-Timeouts

So you can put in your application.conf line like

play.ws.timeout.request = 10 minutes

to specify default timeout for your all requests.

Also you can specify timeout for single request using withRequestTimeout method of WSRequest builder

/**
 * Sets the maximum time you expect the request to take.
 * Use Duration.Inf to set an infinite request timeout.
 * Warning: a stream consumption will be interrupted when this time is reached unless Duration.Inf is set.
 */
def withRequestTimeout(timeout: Duration): WSRequest

So to disable request timeout for a sigle request you can use following code

ws.url(someurl)
      .withMethod("GET")
      .withRequestTimeout(Duration.Inf)
Pahomov Dmitry
  • 111
  • 1
  • 4