Class ServerSentEvent

java.lang.Object
com.chilkatsoft.chilkat2.ChilkatObject
com.chilkatsoft.chilkat2.ServerSentEvent
All Implemented Interfaces:
AutoCloseable

public class ServerSentEvent extends ChilkatObject
<section class="serversentevent-summary">

Parse server-sent event text into event name, data, ID, and retry fields.

Chilkat.ServerSentEvent is a small helper class for parsing Server-Sent Events formatted text. Given a multi-line event block, it extracts the event name, data payload, id value, and retry interval into simple read-only properties. It is useful when an application receives event-stream text and needs to work with the structured event fields instead of manually parsing the raw lines.

Load event text

Use LoadEvent to parse a complete multi-line Server-Sent Event block.

Read event name

Inspect EventName to determine the event type supplied by the server, if an event: field is present.

Read event data

Use Data to obtain the event payload, such as JSON text from a streaming API or real-time service.

Track event IDs

Read LastEventId when the event includes an id: field that identifies the event or stream position.

Handle retry hints

Use Retry to read the server-provided retry interval when a retry: field is included.

Diagnostics

Check LastErrorText if event text cannot be parsed or if a parsing result is unexpected.

Relationship to Chilkat.WebSocket: ServerSentEvent does not manage a WebSocket connection or parse WebSocket frames. Instead, it can be used after Chilkat.WebSocket receives a text message whose payload is formatted as a Server-Sent Event. Read the WebSocket text frame, pass the event text to LoadEvent, then use EventName, Data, LastEventId, and Retry to process the event.

Common pattern: Receive or otherwise obtain a complete Server-Sent Event text block, call LoadEvent, then inspect EventName to route the event and Data to process the payload. Use LastEventId and Retry when the stream provides reconnection or event-position information.

</section>

Instances hold a native Chilkat object: close them with ChilkatObject.close() (try-with-resources) when done; an unclosed instance is freed when it becomes unreachable.

See Also:
Online reference
  • Constructor Details

    • ServerSentEvent

      public ServerSentEvent()
      Creates a new ServerSentEvent object.
  • Method Details

    • getData

      public String getData()
      The data for the server-side event. (If the data field was empty, then this will be empty.)
      Returns:
      the value of the Data property (read-only)
    • getEventName

      public String getEventName()
      The name of the server-side event. (If the event field was not present, then this will be empty.)
      Returns:
      the value of the EventName property (read-only)
    • getLastEventId

      public String getLastEventId()
      The content of the id field, if present.
      Returns:
      the value of the LastEventId property (read-only)
    • getRetry

      public int getRetry()
      The integer value of the retry field, if it exists; otherwise, 0.
      Returns:
      the value of the Retry property (read-only)
    • loadEvent

      public void loadEvent​(String eventText)
      Loads the multi-line event text into this object. For example, the eventText for a Firebase event might look like this:
      event: put
       data: {"path": "/c", "data": {"foo": true, "bar": false}}
      Parameters:
      eventText - the eventText argument (not null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why