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 Akka Source.

    Please see the Server-Sent Events specification for details.

    Example implementation of EventSource in a Controller:

    {{{ //import; //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 = -> EventSource.Event.event(df.format(; return ok().chunked(eventSource.via(EventSource.flow())).as(Http.MimeTypes.EVENT_STREAM); } }}}

    • Constructor Detail

      • EventSource

        public EventSource()