Streaming results using enumerators

Iteratees make it possible to consume streams of data in a non-blocking way. Conversely, enumerators produce data streams in a non-blocking way. They are useful when you need to send large results, or if your result is built from an intermittent data source. An Enumerator[A] object defines a stream of values of type A.

The simplest way to create an enumerator is to use one of the methods of the Enumerator object. For instance, you can easily convert a java.io.InputStream class or a java.io.File class to an Enumerator[Array[Byte]] object as follows:

import play.api.libs.iteratee.Enumerator
Enumerator.fromStream(inputStream)
Enumerator.fromFile(file)

To send a stream of data as a response body, it is better to explicitly ...

Get Play Framework Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.