Skip to content

Instantly share code, notes, and snippets.

@sadache
Created May 17, 2012 08:26
Show Gist options
  • Save sadache/2717373 to your computer and use it in GitHub Desktop.
Save sadache/2717373 to your computer and use it in GitHub Desktop.
Play2: Stream results of parallel jobs as comet to the client
package controllers
import play.api._
import play.api.mvc._
object Application extends Controller {
def index = Action {
import scala.util.Random
import play.api.libs.iteratee._
import play.api.libs.concurrent._
val promises = Range(1,1000).map(i => Promise.timeout(i,Random.nextInt(100)))
val enumerators = promises.map(p => p.map( Enumerator(_))).map(Enumerator.flatten)
val superS = Enumerator.interleave(enumerators)
Ok.stream((superS.map(_.toString) &> play.api.libs.Comet("console.log")) >>> Enumerator.eof)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment