0

The issue

I've got a Rest API in Spring-Boot where i'm listening to an event-source stream from html like so:

 eventSource = new EventSource("/api/events/receive", {
        xhrHeaders: {
            'Content-Type': 'text/event-stream',
            'Connection': 'keep-alive'
        }
    });
 eventSource.onmessage = function (evt) { ... };

The EventSource returns 503 after a while!


Works perfectly on localhost but not on remote host!


Things i've tried

  • add keep-alive on the @RestController via HttpServletResponse Object
@GetMapping(path = "/api/events/receive", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
        public Flux<Ambulance> receiveAmbulance(HttpServletResponse resp) {
            
            resp.addHeader("Connection", "keep-alive");
            

            return Flux.create(sink -> {
                ambulanceProcessor.register(sink::next);
            });
        }
        
  • add to that template a keep-alive header via @Controller
@GetMapping("/dashboard")
    public String getResource(Model model, HttpServletResponse response) {
            response.addHeader("Connection", "keep-alive");
            return "view";
}

Related posted i've visited

  1. How SSE Work
  2. SSE Spring Examples

503 After a few seconds

enter image description here

Note: I've tried almost everything and nothing seems to work for me...

Phill Alexakis
  • 1,449
  • 1
  • 12
  • 31

1 Answers1

1

I manage to keep it alive by creating a new @Service with this @Bean


@Bean
    public void KeepAliveEvt()
    {
        ExecutorService sseMvcExecutor = Executors.newSingleThreadExecutor();
        sseMvcExecutor.execute(() -> {
            try {
                for (int i = 0; true; i++) {
                    Thread.sleep(120000);
                    ambulanceProcessor.process(new Event());
                    System.out.println("Kept Alive");
                    
                }
            } catch (Exception ex) {
              
            }
        });
    }

Basically I'm keeping it alive with a Dummy Event every 2 minutes

Phill Alexakis
  • 1,449
  • 1
  • 12
  • 31