Interface ServerSentEventHandler


public interface ServerSentEventHandler
  • Method Details

    • create

      static ServerSentEventHandler create()
    • create

    • create

    • onConnect

      void onConnect(Request request)
      Called when the client connects to the SSE endpoint.
      Parameters:
      request - The initial request from the client.
    • onDisconnect

      void onDisconnect(Request request)
      Called when the client disconnects from the SSE endpoint.
      Parameters:
      request - The initial request from the client.
    • sendMessage

      default void sendMessage(String message)
      Sends a message to all clients, with the message appended to the `data` field of the Server-Sent Event.
      Parameters:
      message - The message to send.
    • sendMessage

      default void sendMessage(ServerSentEventMessage message)
      Sends a message to all clients, with each field of the ServerSentEventMessage being set in the message, as long as the field is not null.
      Parameters:
      message - The message to send.
    • sendMessage

      void sendMessage(Function<Request,ServerSentEventMessage> messageGenerator)
      Sends a message to all clients, with the message being generated by the provided function. This allows for the message to be tailored per client, if required (e.g. by using path parameters or query parameters).
      Parameters:
      messageGenerator - A function that takes a ServerSentEventRequest and returns a ServerSentEventMessage, that will then be sent to the client represented by the given request.
    • hasActiveConnections

      boolean hasActiveConnections()
      Returns true if there are active connections to the SSE endpoint.
      Returns:
      true if there are active connections to the SSE endpoint.
    • onActive

      void onActive()
      Called when the first client connects to the SSE endpoint. This is a good point to start any background threads that will generate data, which can then be sent by calling the sendMessage methods.
    • onInactive

      void onInactive()
      Called when the last client disconnects from the SSE endpoint. This is a good point to stop any background threads that are generating data.