public class SseEmitter extends ResponseBodyEmitter
ResponseBodyEmitter
for sending
Server-Sent Events.Modifier and Type | Class and Description |
---|---|
static interface |
SseEmitter.SseEventBuilder
A builder for an SSE event.
|
ResponseBodyEmitter.DataWithMediaType
Constructor and Description |
---|
SseEmitter()
Create a new SseEmitter instance.
|
SseEmitter(Long timeout)
Create a SseEmitter with a custom timeout value.
|
Modifier and Type | Method and Description |
---|---|
static SseEmitter.SseEventBuilder |
event() |
protected void |
extendResponse(ServerHttpResponse outputMessage)
Invoked after the response is updated with the status code and headers,
if the ResponseBodyEmitter is wrapped in a ResponseEntity, but before the
response is committed, i.e.
|
void |
send(Object object)
Send the object formatted as a single SSE "data" line.
|
void |
send(Object object,
MediaType mediaType)
Send the object formatted as a single SSE "data" line.
|
void |
send(SseEmitter.SseEventBuilder builder)
Send an SSE event prepared with the given builder.
|
complete, completeWithError, getTimeout, onCompletion, onTimeout
public SseEmitter()
public SseEmitter(Long timeout)
By default not set in which case the default configured in the MVC Java Config or the MVC namespace is used, or if that's not set, then the timeout depends on the default of the underlying server.
timeout
- timeout value in millisecondsprotected void extendResponse(ServerHttpResponse outputMessage)
ResponseBodyEmitter
The default implementation is empty.
extendResponse
in class ResponseBodyEmitter
public void send(Object object) throws IOException
// static import of SseEmitter.* SseEmitter emitter = new SseEmitter(); emitter.send(event().data(myObject));
send
in class ResponseBodyEmitter
object
- the object to writeIOException
- raised when an I/O error occursIllegalStateException
- wraps any other errorspublic void send(Object object, MediaType mediaType) throws IOException
// static import of SseEmitter.* SseEmitter emitter = new SseEmitter(); emitter.send(event().data(myObject, MediaType.APPLICATION_JSON));
send
in class ResponseBodyEmitter
object
- the object to writemediaType
- a MediaType hint for selecting an HttpMessageConverterIOException
- raised when an I/O error occurspublic void send(SseEmitter.SseEventBuilder builder) throws IOException
// static import of SseEmitter SseEmitter emitter = new SseEmitter(); emitter.send(event().name("update").id("1").data(myObject));
builder
- a builder for an SSE formatted event.IOException
- raised when an I/O error occurspublic static SseEmitter.SseEventBuilder event()