Book Image

Play Framework Essentials

By : Julien Richard-Foy
Book Image

Play Framework Essentials

By: Julien Richard-Foy

Overview of this book

Table of Contents (14 chapters)
Play Framework Essentials
About the Author
About the Reviewers

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 class or a class to an Enumerator[Array[Byte]] object as follows:

import play.api.libs.iteratee.Enumerator

To send a stream of data as a response body, it is better to explicitly set the Content-Length response header so that the connection can be kept alive to serve further requests. Alternatively, you can use the chunked transfer encoding as follows:

Ok.chunked(Enumerator.fromFile(new File("foo.txt")))

To use the chunked...