Interface SseClient

All Superinterfaces:
AutoCloseable, Closeable

public interface SseClient extends Closeable
A client for Server-Sent Events (SSE). This class handles the setup of the SSE connection, sending events and comments to the client, and managing the lifecycle of the connection. It ensures proper headers are set and provides methods for sending various types of data.

This class implements Closeable to allow for proper resource management. The connection is automatically closed if the client disconnects or if an error occurs during event emission.

  • Method Details

    • handler

      static ExchangeHandler handler(Consumer<SseClient> consumer)
      Return an SseClient handler.
    • close

      void close()
      Close the SseClient and release keepAlive block if any
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • ctx

      Context ctx()
      Return the request Context.
      Returns:
      the request
    • keepAlive

      void keepAlive()
      By blocking the SSE connection, you can share this client outside the handler to notify it from other sources. Keep in mind that this function will block the handler until the SSE client is released by another thread.
    • onClose

      void onClose(Runnable task)
      Add a callback that will be called either when connection is closed through close(), or when the Emitter is detected as closed.
      Parameters:
      task - task to run
    • sendComment

      void sendComment(String comment)
      Attempt to send a comment. If the Emitter fails to emit (remote client has disconnected), the close() function will be called instead.
    • sendEvent

      void sendEvent(Object data)
      Calls sendEvent(String, Object, String) with event set to "message"
    • sendEvent

      void sendEvent(String event, Object data)
      Calls sendEvent(String, Object, String) with id set to null
    • sendEvent

      void sendEvent(String event, Object data, String id)
      Attempt to send an event. If the Emitter fails to emit (remote client has disconnected), the close() function will be called instead.
      Parameters:
      event - The name of the event.
      data - The data to send in the event. This can be a String, an InputStream, or any object that can be serialized to JSON using the configured JsonService.
      id - The ID of the event.
    • terminated

      boolean terminated()
      Returns true if close() has been called. This can either be by the user, or by Jex upon detecting that the Emitter is closed.