Using Multicast, Publish and IConnectableObservable

Rx operators do not themselves have side effects, but at times, using an Rx object will have a side effect. For example, if we create an IObservable with a COM object, when we subscribe we may add a COM addref, and when we unsubscribe we might end up releasing that COM object. Worse, the effect is different if we have two subscribers than if we have one.

This problem can be eliminated using the Multicast and Publish methods. These methods return a a Connectable Observable that shares a subscription to the underlying source.

The following example demonstrates how side effects can sneak into even a simple program. It uses a Hot Observable (remember, that's a single event-stream), with explicit, ...

Get Programming Reactive Extensions and LINQ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.