5
votes
AskComp: Reactive coding and splitting observables
I was going to ask this on Stackoverflow but it seems like reactive programming is split into per-language questions (RxJava, RxJS, RxRuby, etc.) and this is a more generic question.
How do you stream items from one Observable to multiple Observers?
I have a stream of CSV items, they're mapped to a dictionary/hash table, and then I want to:
- get the maximum value from this stream
- process the stream in a different way
- sample the stream
Can I call subscribe multiple times and then call the other operators?
yes
but should you do it? unless your Observable / "stream of CSV items" is doing some caching, you probably want a layer between your Observable and your multiple Observers to effectively multicast it so that it doesn't "read" your CSV for each subscribe. The RxJS doc nicely talks about this in their Subject section.
True; I was just thinking perhaps I should go with a Subject. Thanks for the link!
I'm trying a Publish right now and then connecting after all the subscribers are added:
This seems to work.
yup,
publish
is really just a wrapper aroundSubject
/multicast
;)Wow, that's useful! I didn't realize source was visible on that site too lol