Search code examples
javaplayframeworkreactive-programmingakka-stream

Play Framework 2.5 Streaming content with delay


I have been trying to stream content through HTTP with Play Framework 2.5 in Java with a delay.

The problem is that I am not sure if the result is actually streaming, that is why I tried to delay each item from emitting which does not seem to work for some reason.

The code

  public Result test(){

    HttpEntity http = new HttpEntity.Streamed(Source.range(0, 99999)
            .map(i -> ByteString.fromString(i.toString()))
            .initialDelay(FiniteDuration.create(200, TimeUnit.MILLISECONDS))
            , Optional.empty(), Optional.of("text/event-stream"));
    return ok().sendEntity(http);
}

The response can be found here.

It does return the value's but it does not delay them, it also sends the whole response after loading for a while. I am not sure if initialDelay is the right operator for a delay.

Is this the right way to send a stream with Play? I have been using this page as reference https://www.playframework.com/documentation/2.5.x/StreamsMigration25

Thank you for helping!


Solution

  • You should use delay, rather than initialDelay, which is just a delay at start of flow. Note that you'll need to define an overflow strategy for when the buffer is full.