o

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 javax.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)
}
Source
EventSource.scala
Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. EventSource
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. Protected

Type Members

  1. case class Event(data: String, id: Option[String], name: Option[String]) extends Product with Serializable

    An event encoded with the SSE protocol..

  2. case class EventDataExtractor[A](eventData: (A) => String) extends Product with Serializable
  3. case class EventIdExtractor[E](eventId: (E) => Option[String]) extends Product with Serializable
  4. case class EventNameExtractor[E](eventName: (E) => Option[String]) extends Product with Serializable
  5. trait LowPriorityEventEncoder extends AnyRef
  6. trait LowPriorityEventIdExtractor extends AnyRef
  7. trait LowPriorityEventNameExtractor extends AnyRef

Value Members

  1. def flow[E](implicit arg0: EventDataExtractor[E], arg1: EventNameExtractor[E], arg2: EventIdExtractor[E]): 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)
  2. object Event extends Serializable
  3. object EventDataExtractor extends with Serializable
  4. object EventIdExtractor extends with Serializable
  5. object EventNameExtractor extends with Serializable