Module io.jooby
Package io.jooby

Interface ServerSentEmitter


public interface ServerSentEmitter
Server-Sent message emitter.

 sse("/pattern", sse -> {
   sse.send("Hello Server-Sent events");
 })

 
Since:
2.5.0
Author:
edgar
  • Method Details

    • onClose

      void onClose(SneakyThrows.Runnable task)
      Generated when client close the connection or when explicit calls to close().
      Parameters:
      task - Cleanup task.
    • getContext

      @NonNull Context getContext()
      Originating HTTP context. Please note this is a read-only context, so you are not allowed to modify or produces a response from it.

      The context let give you access to originating request (then one that was upgrade it).

      Returns:
      Read-only originating HTTP request.
    • getAttributes

      @NonNull default Map<String,Object> getAttributes()
      Context attributes (a.k.a request attributes).
      Returns:
      Context attributes.
    • attribute

      @NonNull default <T> T attribute(@NonNull String key)
      Get an attribute by his key. This is just an utility method around getAttributes(). This method look first in current context and fallback to application attributes.
      Type Parameters:
      T - Attribute type.
      Parameters:
      key - Attribute key.
      Returns:
      Attribute value.
    • attribute

      @NonNull default ServerSentEmitter attribute(@NonNull String key, Object value)
      Set an application attribute.
      Parameters:
      key - Attribute key.
      value - Attribute value.
      Returns:
      This ServerSent.
    • send

      @NonNull default ServerSentEmitter send(@NonNull String data)
      Send a text message to client.
      Parameters:
      data - Text Message.
      Returns:
      This ServerSent.
    • send

      @NonNull default ServerSentEmitter send(@NonNull byte[] data)
      Send a text message to client.
      Parameters:
      data - Text Message.
      Returns:
      This ServerSent.
    • send

      @NonNull default ServerSentEmitter send(@NonNull Object data)
      Send a text message to client.
      Parameters:
      data - Text Message.
      Returns:
      This ServerSent.
    • send

      @NonNull default ServerSentEmitter send(@NonNull String event, @NonNull Object data)
      Send a message to the client and set the event type attribute.
      Parameters:
      event - Event type.
      data - Message.
      Returns:
      This emitter.
    • send

      @NonNull ServerSentEmitter send(@NonNull ServerSentMessage data)
      Send a message to the client.
      Parameters:
      data - Message.
      Returns:
      This emitter.
    • keepAlive

      @NonNull default ServerSentEmitter keepAlive(long time, @NonNull TimeUnit unit)
      Send a comment message to the client. The comment line can be used to prevent connections from timing out; a server can send a comment periodically to keep the connection alive.
      Parameters:
      time - Period of time.
      unit - Time unit.
      Returns:
      This emitter.
    • keepAlive

      @NonNull ServerSentEmitter keepAlive(long timeInMillis)
      Send a comment message to the client. The comment line can be used to prevent connections from timing out; a server can send a comment periodically to keep the connection alive.
      Parameters:
      timeInMillis - Period of time in millis.
      Returns:
      This emitter.
    • getLastEventId

      @Nullable default String getLastEventId()
      Read the Last-Event-ID header and retrieve it. Might be null.
      Returns:
      Last event ID or null.
    • lastEventId

      @Nullable default <T> T lastEventId(Class<T> type)
      Read the Last-Event-ID header and retrieve it. Might be null.
      Type Parameters:
      T - Type to convert.
      Parameters:
      type - Type to convert.
      Returns:
      Last event ID or null.
    • getId

      @NonNull String getId()
      Server-Sent ID. Defaults to UUID.
      Returns:
      Server-Sent ID. Defaults to UUID.
    • setId

      @NonNull ServerSentEmitter setId(@NonNull String id)
      Set Server-Sent ID.
      Parameters:
      id - Set Server-Sent ID.
      Returns:
      This emitter.
    • isOpen

      boolean isOpen()
      True if connection is open.
      Returns:
      True if connection is open.
    • close

      void close()
      Close the ServerSent and fir the close event.