SSE implementation in Spring REST

Can anyone provide an example of SSE (Server Sent Events) using Spring Rest? Basically I have a request and a response will be sent by the server in several chunks. I would like to have a server and client implementation in Spring REST Api without third party rest api like jersey.

+3


source to share


1 answer


There is currently no SSE support in Spring, but it looks like it will be available in version 4.2 which is currently in RC2 You can see the details here https://jira.spring.io/browse/SPR-12212

This works by returning SseEmitter or ResponseBodyEmitter from controller methods.



@RequestMapping(value="/stream", method=RequestMethod.GET)
public ResponseBodyEmitter handle() {
        ResponseBodyEmitter emitter = new ResponseBodyEmitter();
        // Pass the emitter to another component...
        return emitter;
}

// in another thread
emitter.send(foo1);

// and again
emitter.send(foo2);

// and done
emitter.complete();

      

You can see the reference documentation here http://docs.spring.io/spring/docs/4.2.0.RC2/spring-framework-reference/htmlsingle/#mvc-ann-async-http-streaming

+6


source







All Articles