Skip to content

Instantly share code, notes, and snippets.

@dalewking
Last active December 18, 2015 07:09
Show Gist options
  • Save dalewking/5744791 to your computer and use it in GitHub Desktop.
Save dalewking/5744791 to your computer and use it in GitHub Desktop.
Example of streaming text data to client in Scala on Play Framework 2.1 using OutputStream. It took me a long time to figure out how to get this working, so in case it helps someone else, here it is. The >>> Enumerator.eof is very important other wise it hangs
def index = Action {
val output = Enumerator.outputStream
{
o =>
val out = new PrintWriter(new OutputStreamWriter(o, "utf8"), true)
for(i <- 1 to 5)
{
out.println(s"Hello $i")
}
out.close()
}
Ok.stream(output >>> Enumerator.eof).withHeaders(
"Content-Type"->"text/plain; charset=utf-8"
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment