You’ll find bellow a few streaming examples with Play Framework, a kind of small streaming/proxying cheat sheet :)
This examples use the Iteratee API. The main elements from this API are the iteratees (data consumers) and the enumerators (data producers).
If you want to learn more about this API I recommend to read this post.
Stream a file (and transform)
Chunck by chunk
Line by line
HTTP Proxying
In this proxying example we will call an external service in streaming from a Play server, and stream the response to our HTTP clients.
Update : Since Play 2.3, WS provides a getStream method returning a Future[(WSResponseHeaders, Enumerator[Array[Byte]])].
So it’s a little easier to use :
(Thanks Martin for the comment)
Previous solution :
This one deserves an explanation. Concurrent.joined creates an iteratee/enumerator tuple. According to the documentation, “When the enumerator is applied to an iteratee, the iteratee subsequently consumes whatever the iteratee in the pair is applied to. Consequently the enumerator is “one shot”, applying it to subsequent iteratees will throw an exception.”.
WS.url accepts a WSResponseHeaders => Iteratee function to consume the data from the remote service.
In the consumer method we feed the promise with the enumerator (stream) created from the WS result. Then we can stream a response from the enumerator.
(*) This example is inspired by an example from Yann Simon on Github