I'm implementing SSE stream with js EventSource and spring SseEmitter, but every 5 seconds 'onerror' and then 'onopen' events are raised.
This is the server side controller:
@RestController
public class SseController
{
@Autowired
SseService sseService;
@GetMapping(path = "/sseChannel/{userId}", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public SseEmitter openSseChannelForUser(@PathVariable int userId)
{
SseEmitter sseEmitter = new SseEmitter();
sseService.addSse(userId, sseEmitter);
return sseEmitter;
}
}
And this is the javascript:
var sseClient = null;
function sseInit(userId)
{
var url = '/app/spring/sseChannel/'+userId;
sseClient = new EventSource(url);
sseClient.onopen = function ()
{
console.log("onopen() ");
};
sseClient.onmessage = function (evt)
{
console.log("onmessage() "+ evt.data);
};
sseClient.onerror = function (evt)
{
console.log("onerror() ");
};
}
This is the console:
onopen()
onerror()
onopen()
onerror()
onopen()
onerror()
onopen()
onerror()
.
.
.