Class Sse
Server-Sent Events (text/event-stream) from a handler: a response that stays
open and delivers each event to the client the moment it is written.
return Sse.stream(request, events -> {
for (String token : model.tokens(prompt)) {
events.data(token); // on the wire now, not at the end
}
events.event("done").data("{}");
});
The producer runs on the worker that served the request, after the headers
are sent, and the stream ends when it returns. It blocks on its own source --
an upstream stream, a queue it polls -- and should call Sse.Events.ping() when
it has had nothing to send for a while: proxies and load balancers close a
connection that is idle for about a minute. Wait with Thread.sleep, never
Object.wait, which blocks the whole host under virtual threads.
A client that goes away makes the next write throw IOException; let it
propagate so the producer's try-with-resources cleanup runs. A producer that
opens something before it runs should also implement java.io.Closeable: the
server calls close() instead of the producer when no body is sent (HEAD).
To relay an upstream event stream unchanged, HttpProxy already streams it.
This is the blocking-producer model, not Spring's SseEmitter: nothing here
hands events from another thread to the connection.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classThe open stream.static interfaceWrites the events of one stream. -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic HttpServer.Responsestream(HttpServer.Request request, Sse.Producer producer) An event stream answered with status 200.
-
Field Details
-
CONTENT_TYPE
-
-
Method Details
-
stream
An event stream answered with status 200.
-