kheraud
kheraud

Reputation: 5288

Play 2.0, streaming template result?

When using templates to produce the response, the template is computed then the result is sent.

Would it be possible to stream the result of the template while constructing the response ?

Upvotes: 2

Views: 538

Answers (1)

Julien Richard-Foy
Julien Richard-Foy

Reputation: 9663

Streaming works by sending the result part by part, so your template should only build a part of the response.

I assume that you have an Enumerator[Part] where Part is a type modeling a result part. You can build this enumerator from a Web Service call or a database query, for example.

Then you need to transform these responses parts into html parts. You can do that using an Enumeratee[Part, Html].

Finally, use the feed method of your HTTP result and pass it your enumerator combined with your enumeratee.

Here is an (untested) example, illustrating the above explanations:

def stream = Action { implicit request =>
  val stream: Enumerator[Part] = …
  val toHtml = Enumeratee.map[Part] { part =>
    views.html.part(part)
  }
  Ok.feed(stream &> toHtml)
}

Upvotes: 3

Related Questions