org.springframework.web.servlet.mvc.method.annotation

## Class SseEmitter

- java.lang.Object
- - org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter
  - - org.springframework.web.servlet.mvc.method.annotation.SseEmitter

-     public class SseEmitter
      extends ResponseBodyEmitter

  A specialization of `ResponseBodyEmitter` for sending Server-Sent Events.
  Since:
    4.2
  Author:
    Rossen Stoyanchev, Juergen Hoeller, Sam Brannen

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static interface` | `SseEmitter.SseEventBuilder` A builder for an SSE event.

    - ### Nested classes/interfaces inherited from class org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter

      `ResponseBodyEmitter.DataWithMediaType`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `SseEmitter()` Create a new SseEmitter instance.
    `SseEmitter(Long timeout)` Create a SseEmitter with a custom timeout value.

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    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.
    `String` | `toString()`

    - ### Methods inherited from class org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter

      `complete, completeWithError, getTimeout, onCompletion, onError, onTimeout`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### SseEmitter

          public SseEmitter()

      Create a new SseEmitter instance.

    - #### SseEmitter

          public SseEmitter(Long timeout)

      Create a SseEmitter with a custom timeout value.

      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.

      Parameters:
        `timeout` - the timeout value in milliseconds
      Since:
        4.2.2

  - ### Method Detail

    - #### extendResponse

          protected void extendResponse(ServerHttpResponse outputMessage)

      Description copied from class: `ResponseBodyEmitter`
      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. before the response body has been written to.

      The default implementation is empty.

      Overrides:
        `extendResponse` in class `ResponseBodyEmitter`

    - #### send

          public void send(Object object)
                    throws IOException

      Send the object formatted as a single SSE "data" line. It's equivalent to:

           // static import of SseEmitter.*

           SseEmitter emitter = new SseEmitter();
           emitter.send(event().data(myObject));

      Please, see `parent Javadoc` for important notes on exception handling.

      Overrides:
        `send` in class `ResponseBodyEmitter`
      Parameters:
        `object` - the object to write
      Throws:
        `IOException` - raised when an I/O error occurs
        `IllegalStateException` - wraps any other errors

    - #### send

          public void send(Object object,
                           @Nullable
                           MediaType mediaType)
                    throws IOException

      Send the object formatted as a single SSE "data" line. It's equivalent to:

           // static import of SseEmitter.*

           SseEmitter emitter = new SseEmitter();
           emitter.send(event().data(myObject, MediaType.APPLICATION_JSON));

      Please, see `parent Javadoc` for important notes on exception handling.

      Overrides:
        `send` in class `ResponseBodyEmitter`
      Parameters:
        `object` - the object to write
        `mediaType` - a MediaType hint for selecting an HttpMessageConverter
      Throws:
        `IOException` - raised when an I/O error occurs

    - #### send

          public void send(SseEmitter.SseEventBuilder builder)
                    throws IOException

      Send an SSE event prepared with the given builder. For example:

           // static import of SseEmitter
           SseEmitter emitter = new SseEmitter();
           emitter.send(event().name("update").id("1").data(myObject));

      Parameters:
        `builder` - a builder for an SSE formatted event.
      Throws:
        `IOException` - raised when an I/O error occurs

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `ResponseBodyEmitter`

    - #### event

          public static SseEmitter.SseEventBuilder event()