Class SseEventReader

java.lang.Object
org.apache.juneau.marshall.sse.SseEventReader
All Implemented Interfaces:
Closeable, AutoCloseable, Iterator<SseEvent>

public class SseEventReader extends Object implements Iterator<SseEvent>, Closeable
Line-driven Iterator over the events in a text/event-stream.

Wraps a Reader the caller owns and dispatches one SseEvent at a time using the WHATWG parse state machine (HTML ยง9.2). Designed for long-lived SSE streams that outlive any reasonable per-call buffer.

Why a separate class:

No other Juneau marshaller ships a separate iterator-style helper. SSE warrants the deviation because SSE responses are open until the client disconnects or the server pushes its last event; forcing callers through the eager List<SseEvent> path would defeat the entire point of using SSE.

Example:

try (SseEventReader r = new SseEventReader(responseReader)) { while (r.hasNext()) { SseEvent e = r.next(); // Dispatch... } }

Notes:
  • This class is not thread safe.
See Also: