Skip to content
Snippets Groups Projects
Commit 70d8eeb1 authored by Will Billingsley's avatar Will Billingsley
Browse files

Added EventSource example

parent b1266280
No related branches found
No related tags found
No related merge requests found
......@@ -3,7 +3,8 @@ package controllers
import javax.inject._
import akka.stream.{Materializer, OverflowStrategy, ThrottleMode}
import akka.stream.scaladsl.{Flow, Sink, Source, SourceQueueWithComplete}
import akka.stream.scaladsl.{Flow, Keep, Sink, Source, SourceQueueWithComplete}
import play.api.libs.EventSource
import scala.concurrent.duration._
import scala.util.Random
......@@ -150,6 +151,24 @@ class ExamplesController @Inject()(cc: ControllerComponents)(implicit ec: Execut
}
/**
* An example using EventSource (server sent events).
* @return
*/
def eventSource() = Action { request =>
// The source "will be" a queue we can push to. But this doesn't create the queue yet
val s = Source.queue[String](50, OverflowStrategy.backpressure)
// Connect the source via the EventSource's flow.
val stream = s.viaMat(EventSource.flow[String]) {
// It calls us back when the queue has been created!
case (queue, m) =>
listeners.add(queue)
m
}
Ok.chunked(stream).as("text/event-stream")
}
}
......@@ -14,6 +14,8 @@ GET /example/game controllers.ExamplesController.game(id:Optio
GET /example/socket controllers.ExamplesController.websocket()
GET /example/time controllers.ExamplesController.websocketWithHelper()
GET /example/eventsource controllers.ExamplesController.eventSource()
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment