EventSource
play.api.libs.EventSource
object EventSource
This class provides an easy way to use Server Sent Events (SSE) as a chunked encoding, using an Pekko Source.
Please see the Server-Sent Events specification for details.
An example of how to display an event stream:
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import jakarta.inject.Singleton
import org.apache.pekko.stream.scaladsl.Source
import play.api.http.ContentTypes
import play.api.libs.EventSource
import play.api.mvc._
import scala.concurrent.duration._
def liveClock() = Action {
val df: DateTimeFormatter = DateTimeFormatter.ofPattern("HH mm ss")
val tickSource = Source.tick(0 millis, 100 millis, "TICK")
val source = tickSource.map { (tick) =>
df.format(ZonedDateTime.now())
}
Ok.chunked(source via EventSource.flow).as(ContentTypes.EVENT_STREAM)
}
Attributes
- Graph
-
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
EventSource.type
Members list
Type members
Classlikes
object Event
object EventDataExtractor extends LowPriorityEventEncoder
Attributes
- Companion
- class
- Supertypes
-
trait Producttrait Mirrortrait LowPriorityEventEncoderclass Objecttrait Matchableclass AnyShow all
- Self type
-
EventDataExtractor.type
object EventIdExtractor extends LowPriorityEventIdExtractor
Attributes
- Companion
- class
- Supertypes
- Self type
-
EventIdExtractor.type
object EventNameExtractor extends LowPriorityEventNameExtractor
Attributes
- Companion
- class
- Supertypes
- Self type
-
EventNameExtractor.type
trait LowPriorityEventEncoder
Attributes
- Supertypes
-
class Objecttrait Matchableclass Any
- Known subtypes
-
object EventDataExtractor
Attributes
- Supertypes
-
class Objecttrait Matchableclass Any
- Known subtypes
-
object EventIdExtractor
Attributes
- Supertypes
-
class Objecttrait Matchableclass Any
- Known subtypes
-
object EventNameExtractor
Value members
Concrete methods
Makes a Flow[E, Event, _], given an input source.
Makes a Flow[E, Event, _], given an input source.
Usage example:
val jsonStream: Source[JsValue, Unit] = createJsonSource()
Ok.chunked(jsonStream via EventSource.flow).as(ContentTypes.EVENT_STREAM)
Attributes
In this article