I'm using JHipster 3.5.0 with spring-boot and angular to build an application. I would like to send updates from the backend to the UI using server sent events, but I can't get it to work.
Here is the code of my RestController:
@RestController
@RequestMapping("/api")
public class SSEResource {
private final List<SseEmitter> sseEmitters = new CopyOnWriteArrayList<>();
@RequestMapping(value = "/sse", method = RequestMethod.GET)
@Timed
public SseEmitter getSSE() throws IOException {
SseEmitter sseEmitter = new SseEmitter();
this.sseEmitters.add(sseEmitter);
sseEmitter.send("Connected");
return sseEmitter;
}
@Scheduled(fixedDelay = 3000L)
public void update() {
this.sseEmitters.forEach(emitter -> {
try {
emitter.send(String.valueOf(System.currentTimeMillis()));
} catch (IOException e) {
e.printStackTrace();
}
});
}
}
My Angaluar controller looks like this:
(function () {
'use strict';
angular
.module('myApp')
.controller('SSEController', SSEController);
SSEController.$inject = [];
function SSEController() {
var vm = this;
vm.msg = {};
function handleCallback(msg) {
vm.msg = msg.data;
}
vm.source = new EventSource('api/sse');
vm.source.addEventListener('message', handleCallback, false);
}
})
();
When I try to use that code I receive a
406 Not Acceptable HTTP status
because of the request header Accept:"text/event-stream". If I manually change that Header to Accept:"/*" and replay that request using the debug tools of my browser I get
401 Unauthorized HTTP status
I think I'm missing something quite simple, but I allready checked my SecurityConfiguration and authInterceptor without understanding what is missing.
Can anybody explain what I'm doing wrong?