0

Based on the techinque described at this question I have written a basic microservice to provide continuously streaming ByteStrings using akka-http. The pertinent scala code being:

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{Source, Flow}
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.HttpEntity
import akka.http.scaladsl.model.HttpMethods._
import akka.http.scaladsl.model._
import scala.concurrent.Future
import scala.concurrent.duration._

object Server extends App {

  implicit val system = ActorSystem("testServer")
  implicit val materializer = ActorMaterializer()

  val strToChunk = 
    Flow[String].map(ByteString(_))
                .via(Flow[ByteString].map(HttpEntity.ChunkStreamPart(_)))      

  def sourceFactory = 
    Source(0 seconds, 1 seconds,"test").via(strToChunk)

  val requestHandler: HttpRequest => HttpResponse = {
    case HttpRequest(GET, Uri.Path("/stream"), _, _, _) =>
      HttpResponse(entity = HttpEntity.Chunked(ContentTypes.`text/plain`,
                                               sourceFactory))                 
  }

  val bindingFuture = 
    Http().bind(interface = "localhost", port = 8200).runForeach { conn =>
      conn handleWithSyncHandler requestHandler
  }
}

A client makes a single http request and the single response's entity is a chunked stream of ByteStrings, namely "test" every 1 second.

I verified the stream produces "test" values using a scala client. However, a very convenient debugging method would be to point a web browser at the microservice to view the Chunk stream as data comes in. I tried pointing my chrome browser to the port but the browser just hung in the loading/busy state.

So, my question is: how do I modify the HttpResponse so that a browser can display the stream in real-time?

A solution that requires a different HttpResponse entity for browser viewing than for software client consumption is fine (e.g. if the "/stream" address provides an HttpResponse for clients and the "/browserView" provides a different HttpResponse then this is an acceptable answer).

Community
  • 1
  • 1
Ramón J Romero y Vigil
  • 17,373
  • 7
  • 77
  • 125
  • Why are you not using [curl](http://curl.haxx.se/) for debugging? If you really want to know what is going on, this is a great tool. If you change "test" to "test\n" and use `curl -v http://127.0.0.1:8200/stream`, everything works as expected. So I suspect that caching is the culprit. – Rüdiger Klaehn Oct 26 '15 at 13:46
  • Additional to what @RüdigerKlaehn said, does this answer it? https://stackoverflow.com/questions/13557900/chunked-transfer-encoding-browser-behavior – Viktor Klang Oct 26 '15 at 13:50
  • Viktor - That answer looks promising but is missing a key specification, namely : "All headers are set properly". How do i set the header? – Ramón J Romero y Vigil Oct 26 '15 at 13:58

1 Answers1

0

I ended up following Rudiger Klaehn suggestion of using curl instead of a visual browser. Curl was able to display the chunked entity data immediately.

There must be some caching going on in the browser before the render is invoked, and my data chunks were rather small.

Ramón J Romero y Vigil
  • 17,373
  • 7
  • 77
  • 125