I have used this pattern to create ServerSentEvent service: Angular and Server Sent Events Here is service code:
import { Injectable, NgZone } from "@angular/core";
import { Observable } from "rxjs";
@Injectable({
providedIn: "root"
})
export class SseService {
constructor(private _zone: NgZone) { }
getServerSentEvent(url: string): Observable<any> {
return Observable.create(observer => {
const eventSource = this.getEventSource(url);
eventSource.onmessage = event => {
this._zone.run(() => {
observer.next(event.data);
console.log(event); // Here I see every received event
});
};
eventSource.onerror = error => {
this._zone.run(() => {
observer.error(error);
console.log(error);
});
};
// return () => eventSource.close();
});
}
private getEventSource(url: string): EventSource {
return new EventSource(url);
}
}
Then I call this stream of ss-events in main component:
ngOnInit() { //This subscribe works just for the first event no more
this.sseService.getServerSentEvent("http://localhost:8080/ssevents")
.subscribe(
data => {
this.eventmessage = data;
},
error => this.error = error
);
// But direct next EventSource onmessage() call works here:
this.source = new EventSource("http://localhost:8080/ssevents");
this.source.onmessage = event => {
this.zone.run(() => {
this.eventmessage = event.data;
console.log(event);
});
};
this.source.onerror = error => {
this.zone.run(() => {
this.error = error.type;
console.log(error);
});
};
}
Why this internal observable in sseservice does not works? Just first one. So I need use second option - but there I receive permanent error EventTarget (every second or half-second) with no reasons of error description, except Type=error, EventPhase=2. But events are received, that are generated by server just after insertion to mongodb.