Skip to content
This repository has been archived by the owner on Apr 20, 2018. It is now read-only.

Handle reconnection with EventSource #115

Open
davinkevin opened this issue Apr 17, 2016 · 1 comment
Open

Handle reconnection with EventSource #115

davinkevin opened this issue Apr 17, 2016 · 1 comment

Comments

@davinkevin
Copy link

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
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants