Class ServerSentEvents

java.lang.Object
com.linecorp.armeria.server.streaming.ServerSentEvents

public final class ServerSentEvents
extends Object
A utility class which helps to create a Server-Sent Events stream from a content Publisher or Stream.

A user simply creates a streaming HttpResponse which emits Server-Sent Events, e.g.


 Server server =
     Server.builder()
           // Emit Server-Sent Events with the SeverSentEvent instances published by a publisher.
           .service("/sse1",
                    (ctx, req) -> ServerSentEvents.fromPublisher(
                            Flux.just(ServerSentEvent.ofData("foo"), ServerSentEvent.ofData("bar"))))
           // Emit Server-Sent Events with converting instances published by a publisher into
           // ServerSentEvent instances.
           .service("/sse2",
                    (ctx, req) -> ServerSentEvents.fromPublisher(
                            Flux.just("foo", "bar"), ServerSentEvent::ofData))
           .build();
 
  • Method Details

    • fromPublisher

      public static HttpResponse fromPublisher​(org.reactivestreams.Publisher<? extends ServerSentEvent> contentPublisher)
      Creates a new Server-Sent Events stream from the specified Publisher.
      Parameters:
      contentPublisher - the Publisher which publishes the objects supposed to send as contents
    • fromPublisher

      public static HttpResponse fromPublisher​(ResponseHeaders headers, org.reactivestreams.Publisher<? extends ServerSentEvent> contentPublisher)
      Creates a new Server-Sent Events stream from the specified Publisher.
      Parameters:
      headers - the HTTP headers supposed to send
      contentPublisher - the Publisher which publishes the objects supposed to send as contents
    • fromPublisher

      public static HttpResponse fromPublisher​(ResponseHeaders headers, org.reactivestreams.Publisher<? extends ServerSentEvent> contentPublisher, HttpHeaders trailers)
      Creates a new Server-Sent Events stream from the specified Publisher.
      Parameters:
      headers - the HTTP headers supposed to send
      contentPublisher - the Publisher which publishes the objects supposed to send as contents
      trailers - the HTTP trailers
    • fromPublisher

      public static <T> HttpResponse fromPublisher​(org.reactivestreams.Publisher<T> contentPublisher, Function<? super T,​? extends ServerSentEvent> converter)
      Creates a new Server-Sent Events stream from the specified Publisher and converter.
      Parameters:
      contentPublisher - the Publisher which publishes the objects supposed to send as contents
      converter - the converter which converts published objects into ServerSentEvents
    • fromPublisher

      public static <T> HttpResponse fromPublisher​(ResponseHeaders headers, org.reactivestreams.Publisher<T> contentPublisher, Function<? super T,​? extends ServerSentEvent> converter)
      Creates a new Server-Sent Events stream from the specified Publisher and converter.
      Parameters:
      headers - the HTTP headers supposed to send
      contentPublisher - the Publisher which publishes the objects supposed to send as contents
      converter - the converter which converts published objects into ServerSentEvents
    • fromPublisher

      public static <T> HttpResponse fromPublisher​(ResponseHeaders headers, org.reactivestreams.Publisher<T> contentPublisher, HttpHeaders trailers, Function<? super T,​? extends ServerSentEvent> converter)
      Creates a new Server-Sent Events stream from the specified Publisher and converter.
      Parameters:
      headers - the HTTP headers supposed to send
      contentPublisher - the Publisher which publishes the objects supposed to send as contents
      trailers - the HTTP trailers
      converter - the converter which converts published objects into ServerSentEvents
    • fromStream

      public static HttpResponse fromStream​(Stream<? extends ServerSentEvent> contentStream, Executor executor)
      Creates a new Server-Sent Events stream from the specified Stream.
      Parameters:
      contentStream - the Stream which publishes the objects supposed to send as contents
      executor - the executor which iterates the stream
    • fromStream

      public static HttpResponse fromStream​(ResponseHeaders headers, Stream<? extends ServerSentEvent> contentStream, Executor executor)
      Creates a new Server-Sent Events stream from the specified Stream.
      Parameters:
      headers - the HTTP headers supposed to send
      contentStream - the Stream which publishes the objects supposed to send as contents
      executor - the executor which iterates the stream
    • fromStream

      public static HttpResponse fromStream​(ResponseHeaders headers, Stream<? extends ServerSentEvent> contentStream, HttpHeaders trailers, Executor executor)
      Creates a new Server-Sent Events stream from the specified Stream.
      Parameters:
      headers - the HTTP headers supposed to send
      contentStream - the Stream which publishes the objects supposed to send as contents
      trailers - the HTTP trailers
      executor - the executor which iterates the stream
    • fromStream

      public static <T> HttpResponse fromStream​(Stream<T> contentStream, Executor executor, Function<? super T,​? extends ServerSentEvent> converter)
      Creates a new Server-Sent Events stream from the specified Stream and converter.
      Parameters:
      contentStream - the Stream which publishes the objects supposed to send as contents
      executor - the executor which iterates the stream
      converter - the converter which converts published objects into ServerSentEvents
    • fromStream

      public static <T> HttpResponse fromStream​(ResponseHeaders headers, Stream<T> contentStream, Executor executor, Function<? super T,​? extends ServerSentEvent> converter)
      Creates a new Server-Sent Events stream from the specified Stream and converter.
      Parameters:
      headers - the HTTP headers supposed to send
      contentStream - the Stream which publishes the objects supposed to send as contents
      executor - the executor which iterates the stream
      converter - the converter which converts published objects into ServerSentEvents
    • fromStream

      public static <T> HttpResponse fromStream​(ResponseHeaders headers, Stream<T> contentStream, HttpHeaders trailers, Executor executor, Function<? super T,​? extends ServerSentEvent> converter)
      Creates a new Server-Sent Events stream from the specified Stream and converter.
      Parameters:
      headers - the HTTP headers supposed to send
      contentStream - the Stream which publishes the objects supposed to send as contents
      trailers - the HTTP trailers
      executor - the executor which iterates the stream
      converter - the converter which converts published objects into ServerSentEvents
    • fromEvent

      public static HttpResponse fromEvent​(ServerSentEvent sse)
      Creates a new Server-Sent Events stream of the specified content.
      Parameters:
      sse - the ServerSentEvent object supposed to send as contents
    • fromEvent

      public static HttpResponse fromEvent​(ResponseHeaders headers, ServerSentEvent sse)
      Creates a new Server-Sent Events stream of the specified content.
      Parameters:
      headers - the HTTP headers supposed to send
      sse - the ServerSentEvent object supposed to send as contents
    • fromEvent

      public static HttpResponse fromEvent​(ResponseHeaders headers, ServerSentEvent sse, HttpHeaders trailers)
      Creates a new Server-Sent Events stream of the specified content.
      Parameters:
      headers - the HTTP headers supposed to send
      sse - the ServerSentEvent object supposed to send as contents
      trailers - the HTTP trailers