Interface ServerSentEventParser

All Known Implementing Classes:
DefaultServerSentEventParser

public interface ServerSentEventParser
Parses server-sent events (SSE) from an InputStream, constructs ServerSentEvent objects, and delivers them to the provided ServerSentEventListener.
  • Method Details

    • parse

      void parse(InputStream httpResponseBody, ServerSentEventListener listener)
      Parses an input stream containing server-sent events and notifies the listener of parsed events. This method blocks until the input stream is exhausted or an error occurs.

      For each complete event found in the stream, ServerSentEventListener.onEvent(ServerSentEvent) is called. If any parsing or processing error occurs, ServerSentEventListener.onError(Throwable) is called and parsing may terminate.

      Parameters:
      httpResponseBody - the input stream containing SSE data to parse
      listener - the listener to receive parsed events or error notifications
    • incremental

      @Experimental default ServerSentEventParser.Incremental incremental()
      Returns a fresh ServerSentEventParser.Incremental parser instance backed by the same parsing logic as parse(InputStream, ServerSentEventListener), but driven by byte chunks instead of a blocking InputStream. Used by the publisher-based HTTP client API to avoid pinning a thread per active stream.

      A parser opts into the publisher path by overriding this method. One that has not opted in throws AsyncNotSupportedException, the same "this component is not asynchronous" marker the rest of the non-blocking API reports through its return type - this method returns a plain value, so it has nowhere to carry a failure and must throw. Existing parsers that only support parse(InputStream, ServerSentEventListener) continue to work unchanged.

      Throws:
      dev.langchain4j.exception.AsyncNotSupportedException - if this parser does not support incremental parsing
      Since:
      1.20.0