Class EventSource
- Object
-
- play.libs.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); } }}}
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
EventSource.Event
Utility class to build events.
-
Constructor Summary
Constructors Constructor Description EventSource()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Flow<EventSource.Event,ByteString,?>
flow()
-
-
-
Method Detail
-
flow
public static Flow<EventSource.Event,ByteString,?> flow()
- Returns:
- a flow of EventSource.Event to ByteString.
-
-