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 Object
trait Matchable
class Any
Self type

Members list

Type members

Classlikes

case class Event(data: String, id: Option[String], name: Option[String])

An event encoded with the SSE protocol..

An event encoded with the SSE protocol..

Attributes

Companion
object
Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
object Event

Attributes

Companion
class
Supertypes
trait Product
trait Mirror
class Object
trait Matchable
class Any
Self type
Event.type
case class EventDataExtractor[A](eventData: A => String)

Attributes

Companion
object
Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all

Attributes

Companion
class
Supertypes
trait Product
trait Mirror
class Object
trait Matchable
class Any
Show all
Self type
case class EventIdExtractor[E](eventId: E => Option[String])

Attributes

Companion
object
Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all

Attributes

Companion
class
Supertypes
trait Product
trait Mirror
class Object
trait Matchable
class Any
Show all
Self type
case class EventNameExtractor[E](eventName: E => Option[String])

Attributes

Companion
object
Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all

Attributes

Companion
class
Supertypes
trait Product
trait Mirror
class Object
trait Matchable
class Any
Show all
Self type

Attributes

Supertypes
class Object
trait Matchable
class Any
Known subtypes

Attributes

Supertypes
class Object
trait Matchable
class Any
Known subtypes

Attributes

Supertypes
class Object
trait Matchable
class Any
Known subtypes

Value members

Concrete methods

def flow[E : EventIdExtractor]: Flow[E, Event, _]

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