Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

266
Views
Spring boot: SSE Server Sent Events not working

In Spring Boot, when we try to send a Server Sent Event, it only sends an error event containing data: {"timeout":-1} when we try to connect, and the connection closes. The Spring Boot class is as follows

@RestController
@CrossOrigin(origins = "*")
public class SsePushNotificationRestController {
    private static final Logger log = LoggerFactory.getLogger(SsePushNotificationRestController.class);
    private SseEmitter emitter;

    @GetMapping("/test")
    public String getString(){
        try {
            emitter.send("User connected");
            log.info("User connected");
            emitter.complete();
        } catch (Exception e) {
            log.info("Error while sending message to client: " + e.getMessage());
        }
        return "placeholder";
    }

    @GetMapping("/emitter")
    public SseEmitter eventEmitter(@RequestParam String userId) {
        emitter = new SseEmitter(-1L);
        return emitter;
    }
}

And our client code is as follows:

const eventSource = new EventSource('http://localhost:8080/emitter?userId=testUser');

eventSource.addEventListener("message", (event) => {
    console.log(event);
});

eventSource.addEventListener("open", (event) => {
    console.log("connection opened");
});

eventSource.addEventListener("error", (e) => {
    if (e.readyState === EventSource.CLOSED) {
        console.log('closed');
    }
    else {
        console.log(e);
    }
    e.target.close();
});

document.getElementById("btn").onclick = e => {
    fetch('http://localhost:8080/test').then( data => console.log(data)).catch(data => console.log(data));
};

Immediately, an error is created before we can click the button to generate an event. error What could be wrong?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

What does your Spring boot terminal say? I think I need that information to address your program's error. By the way allowing cross origin resources sharing for requests from any sources (using wildcard) is a very very bad practice.

One possible reason of error is something's wrong when you create an instance of SSEemitter. (new SSeEmitter(-1L))

SSeEmitter(Long timeout) is creating server side event with set timeout it says. So if timeout is -1, I guess it would immediately be timed out and return timeout response. So it wouldn't be error, just working as written

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!