I am writing a web which as client side and using spring boot to write for server side for SSE using SseEmitter. However, when client first connected to server side, I can see the debugger executed smoothly at server side, when upon return emitter, client side onmessage received timeout: message. Can guide me if I did anything wrong? below are my coding, for both server and client side.
server:
@RestController
@RequestMapping("/")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class RateDataController {
private final CopyOnWriteArrayList<SseEmitter> emitters = new CopyOnWriteArrayList<>();
@GetMapping(value = "/subscribe", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public SseEmitter subscribe() {
SseEmitter emitter = new SseEmitter(Long.MAX_VALUE);
try {
emitter.send(emitter.event().name("INIT"));
} catch (IOException e) {
e.printStackTrace();
}
emitters.add(emitter);
emitter.onCompletion(() -> emitters.remove(emitter));
return emitter;
}
Client side:
eventSource = new EventSource(
'http://localhost:8080/subscribe')
eventSource.onopen = (event) => {
console.log('connection opened')
}
eventSource.onmessage = (event) => {
console.log('result', event.data)
}
Image on message received from server
Any advice will be appreciated. Thanks,