Search code examples
scalaplayframeworkstreamingakkaakka-stream

Cannot stream simple string in Play 2.5 (Scala)


I'm really struggling to stream a simple string in Play 2.5. So if I wanted to output the string "Hello" I might start with this:

package controllers

import javax.inject.Inject
import play.api.mvc.{Action, Controller}
import akka.stream.scaladsl.Source


class Enum @Inject() extends Controller {

  def index = Action {
    Ok.chunked(Source("hello"))
  }
}

But clearly this doesn't compile. I have read the Play documentation regarding streaming and I know that in previous versions Ok.chunked(Enumerator("hello")) would have been the way. And unfortunately Play's migration guide hasn't clarified any of this to me. Maybe I've been looking too long at this screen.


Solution

  • The problem is that Source("hello") is a source of Char, because Source() takes a Seq.

    Since Char are not Writeable (in the sense of play.api.http.Writeable), you cannot give a source of Char to Ok.chunked

    If you want to send just one String element, you should do Source.single("hello")