1

in the following web application we want the client to subscribe for update events, using SSE:

Server:

@GetMapping("/update/{userId}")
@CrossOrigin
public SseEmitter subscribeUpdate(@PathVariable("userId") Long userId) {

    SseEmitter sseEmitter = new SseEmitter(SUBSCRIBER_LIFETIME);

    sseEmitter.onCompletion(() -> LOGGER.info("SseEmitter is completed"));

    sseEmitter.onTimeout(() -> userService.removeSubscriber(userId));

    sseEmitter.onError((ex) -> LOGGER.info("SseEmitter got error"));

    userService.putSubscriber(userId, sseEmitter);

    LOGGER.info("Controller exits");
    return sseEmitter;
}

Client:

 async subscribeSSE() {

    let userId = 1; //test

    let eventSource = new EventSource(`http://localhost:8080/update/${userId}`);

    eventSource.onopen = (event) => {console.log("connection opened")}

    eventSource.onmessage = (event) => {/* do stuff */}

    eventSource.onerror = (event) => {console.log(event.target.readyState)}
}

now when running this, the server seems to successfully create the SseEmitter (according to the log), but it never reaches the client. There we only get a 503 error (service unavailable). Any ideas why we don't get the object we requested, even though the server doesn't have any trouble with it?

EDIT: problem found. The SseEmitter itself was created successfully. It was the internal update method that caused the error. Got confused because in the console it really looked like nothing was returned, but the SseEmitter made it save to the client and then instantly caused an exception because of the first message it tried to send.

Duke
  • 386
  • 2
  • 13
  • 1
    Try to test your /update/ with another client instead react to validate if spring is fine. You could use: https://stackoverflow.com/a/45087364/3957754 – JRichardsz May 21 '21 at 14:20

0 Answers0