Package ratpack.sse
Class ServerSentEvents
- java.lang.Object
-
- ratpack.sse.ServerSentEvents
-
- All Implemented Interfaces:
Renderable
public class ServerSentEvents extends java.lang.Object implements Renderable
Arenderable
object for streaming server side events.A
renderer
for this type is implicitly provided by Ratpack core.Example usage:
import org.reactivestreams.Publisher; import ratpack.http.client.ReceivedResponse; import ratpack.sse.ServerSentEvents; import ratpack.test.embed.EmbeddedApp; import java.time.Duration; import java.util.Arrays; import java.util.Objects; import static ratpack.sse.ServerSentEvents.serverSentEvents; import static ratpack.stream.Streams.periodically; import static java.util.stream.Collectors.joining; import static org.junit.Assert.assertEquals; public class Example { public static void main(String[] args) throws Exception { EmbeddedApp.fromHandler(context -> { Publisher<String> stream = periodically(context, Duration.ofMillis(5), i -> i < 5 ? i.toString() : null ); ServerSentEvents events = serverSentEvents(stream, e -> e.id(Objects::toString).event("counter").data(i -> "event " + i) ); context.render(events); }).test(httpClient -> { ReceivedResponse response = httpClient.get(); assertEquals("text/event-stream;charset=UTF-8", response.getHeaders().get("Content-Type")); String expectedOutput = Arrays.asList(0, 1, 2, 3, 4) .stream() .map(i -> "id: " + i + "\nevent: counter\ndata: event " + i + "\n") .collect(joining("\n")) + "\n"; assertEquals(expectedOutput, response.getBody().getText()); }); } }
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Publisher<? extends Event<?>>
getPublisher()
The stream of events.void
render(Context context)
Render this object to the response.static <T> ServerSentEvents
serverSentEvents(Publisher<T> publisher, Action<? super Event<T>> action)
Creates a new renderable object wrapping the event stream.
-
-
-
Method Detail
-
serverSentEvents
public static <T> ServerSentEvents serverSentEvents(Publisher<T> publisher, Action<? super Event<T>> action)
Creates a new renderable object wrapping the event stream.Takes a publisher of any type, and an action that mutates a created
Event
object for each stream item. The action is executed for each item in the stream as it is emitted before being sent as a server sent event. The state of the event object when the action completes will be used as the event.The action MUST set one of
id
,event
,data
orcomment
.- Type Parameters:
T
- the type of object in the event stream- Parameters:
publisher
- the event streamaction
- the conversion of stream items to event objects- Returns:
- a
renderable
object
-
getPublisher
public Publisher<? extends Event<?>> getPublisher()
The stream of events.- Returns:
- the stream of events
-
render
public void render(Context context) throws java.lang.Exception
Render this object to the response.- Specified by:
render
in interfaceRenderable
- Parameters:
context
- the request handling context- Throws:
java.lang.Exception
- any
-
-