I have a Observable from a server sent event which returns data und errors using the following solution Allow creation of Observable from EventSource (server-sent events): enhancement
To solve connection issues I want to separate the
const eventStream = new EventSource(evtSourceAdress);
part into an separate Observable
if connection succeeds do further logic
using the solution from
streamHandler(evtSourceAdress: string) {
return new Observable<MessageEvent<any>>(subscriber => {
const eventStream = new EventSource(evtSourceAdress);
eventStream.onmessage = e => subscriber.next(e);
eventStream.onerror = e => subscriber.error(e);
/* add if else clause if error is returned */
return () => {
if (eventStream.readyState === 1) {
/* TODO add reconnect */
eventStream.close()
}
}
})
}
I modified the code to get the eventStream as an Observable
openEventSource(url: string): Observable<any> {
return new Observable<MessageEvent>(subscribe => {
const sse = new EventSource(url);
// return sse.readyState
})
// return new Observable<MessageEvent>(subscriber => {
// const sse = new EventSource(url);
}
this.eventStreamHandlerServ.openEventSource('http://localhost:3000/sse/event').subscribe(e => console.log(e))
which does not return anything
adding a return statement return sse.readyState
gives me Type 'number' is not assignable to type 'TeardownLogic'.
is there a way to only get the event as an observable ?
considered answers Creating an RxJS Observable from a (server sent) EventSource