Interface EventSink


public interface EventSink
The destination of a server-sent event stream (see Response.sse(com.jsgalactic.axiom.http.EventStreamBody)). It has the lifecycle and the failure behavior of the BodyWriter it wraps: valid only while the EventStreamBody runs, blocking under backpressure, and throwing a StreamAbortedException once the stream cannot be written. Each call sends one complete event in a single write.
  • Method Details

    • send

      void send(ServerSentEvent event) throws IOException
      Sends an event.
      Parameters:
      event - the event
      Throws:
      StreamAbortedException - if the stream was aborted
      IOException - if the stream cannot be written for another reason
    • send

      default void send(String data) throws IOException
      Sends an event that has only data.
      Parameters:
      data - text of the event; line breaks become separate data: lines
      Throws:
      StreamAbortedException - if the stream was aborted
      IOException - if the stream cannot be written for another reason
    • comment

      void comment(String text) throws IOException
      Sends a comment, which clients ignore. Line breaks in the text become separate comment lines, so the text cannot inject fields.
      Parameters:
      text - comment text
      Throws:
      StreamAbortedException - if the stream was aborted
      IOException - if the stream cannot be written for another reason
    • keepAlive

      default void keepAlive() throws IOException
      Sends a comment that keeps the connection from looking idle to clients and intermediaries. Call it at an interval shorter than their idle timeouts when events are rare. It also discovers a client that has gone away, by failing with a StreamAbortedException.
      Throws:
      StreamAbortedException - if the stream was aborted
      IOException - if the stream cannot be written for another reason
    • bytesWritten

      long bytesWritten()
      Returns the number of bytes sent so far, counted against the stream's byte cap.
      Returns:
      bytes written
    • shutdownRequested

      default boolean shutdownRequested()
      Reports whether the listener has started to shut down; see BodyWriter.shutdownRequested(). An event stream that sees it should send a last event and return, which ends the stream cleanly; browsers then reconnect after the retry delay.
      Returns:
      true once shutdown has begun
    • onShutdown

      default void onShutdown(Runnable action)
      Runs an action once when shutdown begins; see BodyWriter.onShutdown(Runnable).
      Parameters:
      action - what to run; quick and non-blocking
    • of

      static EventSink of(BodyWriter out)
      Wraps a body writer as an event sink.
      Parameters:
      out - destination of the encoded events
      Returns:
      a sink writing UTF-8 encoded events to it