Reactive-Extensions / RxJS-DOM

HTML DOM Bindings for the Reactive Extensions for JavaScript
http://reactivex.io
Other
438 stars 101 forks source link

Handle reconnection with EventSource #115

Open davinkevin opened 8 years ago

davinkevin commented 8 years ago

Hi,

I would like to use RxJS Dom and Server Sent Event to replace my old web-socket implementation. With native EventSource, the following code re-establish a connection every 30 seconds (depending of the backend I think)

let source = new EventSource('/api/task/downloadManager/downloading');
source.onmessage = m => console.log(m);

So I can use this system for constant communication from backend to client (for monitoring change on backend ressources).

But, when I use the Rx.DOM, the connection is correctly initiated but the eventSource is never relaunch, I just have the error due to timeout.

/* Declaration of Observable */
this.downloadings$ = Rx.DOM.fromEventSource('/api/task/downloadManager/downloading')
this.donwloadingSubscription = this.DonwloadManager.downloading$
            .subscribe(
                m => { console.log(JSON.parse(m)); },
                e => { try{ console.log(e); } catch (e){} }
            );

And after 30 second, I have the onError callback triggered... with the e event which is an exception with type error. The observable totally stops after that, I don't see any request to the backend (unlike the default EventSource implementation).

I've done an usage with Rx.Observable.create, and it seems to work :

this.downloading$ = Rx.Observable.create(o => {
            let s = new EventSource('/api/task/downloadManager/downloading');
            s.onmessage = m => o.onNext(console.log(JSON.parse(m.data)));
            s.onerror = e => o.onError(console.log(e));

            // Note that this is optional, you do not have to return this if you require no cleanup
            return function () {
                s.close();
            };
        });

Could you help or say me if this behavior without re-connection is intentional or if it's a bug ?

Thanks for your help

ErunamoJAZZ commented 8 years ago

We have this same problem. I think its actually a bug, since EventSource support reconnecting, I think RxJS must support it too.