Skip to content

Instantly share code, notes, and snippets.

@pdemanget
Created February 1, 2018 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pdemanget/b6bbef7d5239089cedbe2de40e2f4863 to your computer and use it in GitHub Desktop.
Save pdemanget/b6bbef7d5239089cedbe2de40e2f4863 to your computer and use it in GitHub Desktop.
Server Sent event
/**
* Server sent event dom using Spring webflux.
*
* @author Philippe.DEMANGET
*/
@RestController
@RequestMapping("/event/stream")
public class EventStreamController {
private Logger logger = LoggerFactory.getLogger(getClass());
private List<FluxSink<Line>> fluxSinks = new ArrayList<>();
/**
* Simule une alarme toutes les 10 s
*/
@Scheduled(fixedRate = 1_000)
public void sendAlarm() {
logger.trace("SEND2");
System.out.println("SEND2");
fluxSinks.forEach(fluxSink -> {
fluxSink.next(new Line());
});
}
/**
* Flux d'évènements en SSE.
* @return
*/
//@GetMapping( produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
@GetMapping( produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<Line> stockTransactionEvents() {
return Flux.create(fluxSink -> {
fluxSinks.add(fluxSink);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment