0
votes

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()
.
.
.
1

1 Answers

1
votes

It was the timeout

@GetMapping(path = "/sseChannel/{userId}"/* , produces = MediaType.TEXT_EVENT_STREAM_VALUE */)
    public SseEmitter openSseChannelForUser(@PathVariable int userId)
    {
        Long timeout = (long) (1000*60);
        SseEmitter sseEmitter = new SseEmitter(timeout);
        sseService.addSse(userId, sseEmitter);

         sseEmitter.onCompletion(() -> {
             synchronized (new Object()) {
                 sseService.removeSse(userId);
             }
         });

         sseEmitter.onTimeout(()-> {
             sseEmitter.complete();
         });

        return sseEmitter;
    }