Reconnecting WebSocket with shared RxJS observable

I observe something like this:

const records$ =
    Rx.DOM.fromWebSocket('ws://192.168.2.4:9001/feed/', null)
    .map(ev => parseRecord(ev.data))
    .share();

      

I have a lot of subscribers. When the connection is lost, all subscribers unsubscribe:

let records$Subscription;
records$Subscription = records$.subscribe(
    record => { ... },
    error => records$Subscription.dispose()
);

      

I have verified that the call is dispose

indeed made once per subscription. Hence, the value has share

reached zero.

However, when I subscribe again records$

, no new WebSocket connection is established. However, when I remove the call share

it does. Why doesn't this work with share

?

0


source to share


1 answer


I believe in rxjs v5, share

allows you to reconnect, but not in Rxjs v4.

In Rxjs 4, share

is basic multicast.refCount

, and once the object used for multicasting is complete, it cannot be reused (according to the rules of the Rxjs grammar, see What are the semantics of different RxJS subjects? ), Which leads to the behavior you observe.

In Rxjs 5, it uses a factory object (something like multicast(() => new Rx.Suject().refCount())

), so the subject is recreated when needed.



See issues here and here for more details.

In short, if you can't do with the current behavior, you can switch to v5 (note that it is still in beta and there are some violations).

+1


source







All Articles