Created
May 17, 2012 08:26
-
-
Save sadache/2717373 to your computer and use it in GitHub Desktop.
Play2: Stream results of parallel jobs as comet to the client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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