Package play.libs

Class EventSource


  • public class EventSource
    extends Object
    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.

    Example implementation of EventSource in a Controller:

    {{{ //import org.apache.pekko.stream.javadsl.Source; //import play.mvc.*; //import play.libs.*; //import java.time.ZonedDateTime; //import java.time.format.*; //import scala.concurrent.duration.Duration; //import static java.util.concurrent.TimeUnit.*; //import static play.libs.EventSource.Event.event; //private final DateTimeFormatter df = DateTimeFormatter.ofPattern("HH mm ss");

    public Result liveClock() { Source<String, ?> tickSource = Source.tick(Duration.Zero(), Duration.create(100, MILLISECONDS), "TICK"); Source<EventSource.Event, ?> eventSource = tickSource.map((tick) -> EventSource.Event.event(df.format(ZonedDateTime.now()))); return ok().chunked(eventSource.via(EventSource.flow())).as(Http.MimeTypes.EVENT_STREAM); } }}}

    • Constructor Detail

      • EventSource

        public EventSource()