I am using Spring mvc 4.3.30 version and Angular 11. I would like to subscribe to real time updates and it would be always unidirectional (From server to Ui) hence i opted for SSE.
Below is my code JAVA SPRING
@RestController
@RequestMapping(path = "/api/v1/cite-located-sse")
public class CiteLocatedDeltasSSEController
{
@GetMapping(value="deltas-listener")
public SseEmitter citeLocatedDeltasListener() {
SseEmitter emitter = new SseEmitter(12000L);
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(() -> {
try {
emitter.send("Hello!");
} catch (Exception ex) {
emitter.completeWithError(ex);
}
finally
{
emitter.complete();
}
});
executor.shutdown();
return emitter;
}
}
Angular 11 Event Source Listener
@Injectable({
providedIn: 'root'
})
export class EventSourceService{
private baseUrl = 'cite-located-sse';
constructor(private urlBuilder: UrlBuilder) { }
connect(): void {
console.log('Connecting to SSE...');
const url = "http://localhost:8080/api/v1/cite-located-sse/deltas-listener";
const eventSource = new EventSource(url);
eventSource.addEventListener('message', (event: MessageEvent) => {
console.log('Received event: ', event)
});
eventSource.onerror = (error: Event) => {
console.log('EventSource error: ', error);
eventSource.close();
};
}
}
I am using Tomcat 8.5 version, but i am receiving the below error at the client side.