Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSE implementation in Spring REST

Can anybody provide an example for SSE ( Server sent events) using Spring Rest ? Basically i have a request and the response for it would be sent by the server in multiple chunks. I would like to have the server and client implementation in Spring REST Api without third party rest api like jersey.

like image 529
Vivek Avatar asked Jul 05 '15 09:07

Vivek


1 Answers

There isn't any direct support for SSE in Spring currently but it looks like it will be available in 4.2 which is in RC2 right now You can see the details here https://jira.spring.io/browse/SPR-12212

This works via returning an SseEmitter or a ResponseBodyEmitter from the 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

like image 185
tobad357 Avatar answered Sep 20 '22 12:09

tobad357