Currently, as part of an Angular 8 project with Electron 6 and Ionic 4, we are in the evaluation phase of deciding whether to replace polling with either SSE (Server-sent events) or Web Sockets. My task involves researching SSE.
To test SSE, I set up a small Express application that generates random numbers. Everything is functioning properly, but I am facing challenges when it comes to reconnecting on server errors.
This is how my implementation looks:
private createSseSource(): Observable<MessageEvent> {
return Observable.create(observer => {
this.eventSource = new EventSource(SSE_URL);
this.eventSource.onmessage = (event) => {
this.zone.run(() => observer.next(event));
};
this.eventSource.onopen = (event) => {
console.log('connection open');
};
this.eventSource.onerror = (error) => {
console.log('decided not to take any action now');
// this.zone.run(() => observer.error(error));
// this.closeSseConnection();
// this.reconnectOnError();
};
});
}
I attempted to implement the reconnectOnError()
function based on this answer, but unfortunately, I couldn't get it to work. Eventually, I opted to skip the reconnectOnError()
function, which seems to be the better approach. Rather than closing and reopening the connection or propagating the error to the observable, awaiting for the server to automatically reconnect upon running again appears more suitable.
My question is: Is this truly the best course of action? It is worth noting that the frontend app communicates exclusively with its own server, which cannot be accessed by another instance of the application (embedded device).