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?

4 comments

  1. [4]
    zowesiouff
    Link
    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...

    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.

    3 votes
    1. [3]
      SourceContribute
      Link Parent
      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: source =...

      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:

      source = Observable.from_iterable(read_csv(filename))
      published = source.map(...).publish()
      published.max().subscribe()
      published.group_by().subscribe()
      published.connect()
      

      This seems to work.

      2 votes
      1. [2]
        zowesiouff
        Link Parent
        yup, publish is really just a wrapper around Subject / multicast ;)

        yup, publish is really just a wrapper around Subject / multicast ;)

        2 votes
        1. SourceContribute
          Link Parent
          Wow, that's useful! I didn't realize source was visible on that site too lol

          Wow, that's useful! I didn't realize source was visible on that site too lol

          1 vote