Event source connection can not be kept alive on remote host Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Event source connection can not be kept alive on remote host without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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 -> {
  • add to that template a keep-alive header via @Controller
    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…


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

    public void KeepAliveEvt()
        ExecutorService sseMvcExecutor = Executors.newSingleThreadExecutor();
        sseMvcExecutor.execute(() -> {
            try {
                for (int i = 0; true; i++) {
                    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

We are here to answer your question about Event source connection can not be kept alive on remote host - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji