Class ServerSentEvent

java.lang.Object
com.jsgalactic.axiom.http.ServerSentEvent

public final class ServerSentEvent extends Object
One event of a server-sent event stream (see Response.sse(com.jsgalactic.axiom.http.EventStreamBody)). Immutable.

An event is framed on the wire as event:, id:, retry: and one data: line per line of the data, followed by a blank line. Line breaks are what separate fields in this format, so the text of a field must never be able to start a new one:

  • the data may contain line breaks (CR, LF or CRLF); each line is sent as its own data: line, and the client joins them again with LF, so no input can add a field;
  • the event name and the id may not contain CR or LF (nor NUL, for the id), and such a value is rejected with IllegalArgumentException rather than altered.
  • Method Details

    • data

      public static ServerSentEvent data(String data)
      Creates an event with data and the default event name message.
      Parameters:
      data - text of the event; may be empty and may contain line breaks
      Returns:
      the event
    • named

      public static ServerSentEvent named(String name, String data)
      Creates an event with a name, which clients select with addEventListener.
      Parameters:
      name - event name; not empty, without CR or LF
      data - text of the event; may be empty and may contain line breaks
      Returns:
      the event
      Throws:
      IllegalArgumentException - if the name is empty or contains CR or LF
    • withId

      public ServerSentEvent withId(String id)
      Returns a copy with an event ID, which a reconnecting client returns in Last-Event-ID.
      Parameters:
      id - event ID; without CR, LF or NUL; empty resets the client's last ID
      Returns:
      the event
      Throws:
      IllegalArgumentException - if the ID contains CR, LF or NUL
    • withRetry

      public ServerSentEvent withRetry(Duration retry)
      Returns a copy that tells the client how long to wait before reconnecting.
      Parameters:
      retry - reconnection delay, from one millisecond to Integer.MAX_VALUE milliseconds
      Returns:
      the event
      Throws:
      IllegalArgumentException - if the delay is outside that range
    • name

      public String name()
      Returns the event name.
      Returns:
      the name, or null for the default event
    • id

      public String id()
      Returns the event ID.
      Returns:
      the ID, or null when none is sent
    • retry

      public Duration retry()
      Returns the reconnection delay.
      Returns:
      the delay, or null when none is sent
    • data

      public String data()
      Returns the data as given.
      Returns:
      the data
    • equals

      public boolean equals(Object other)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object