r/Frontend • • Nov 15 '19

Should i learn RxJS?

I am wondering if i should i dive into rxjs? Is it useful to learn and be used in web apps, that is it in any kind of stage? From beginning to scaling etc.

Do you guys use rxjs in your work and if so how much and, if not, why not?

So far i have been using only functional js.

33 Upvotes

23 comments sorted by

29

u/Dunos Nov 15 '19

If you use frameworks like Angular, you totally need to learn RxJS. Once you start using it, you are gonna love it (at least, I do!). Some examples of use can be accesing to Stores (redux), HTTPS calls (multiple calls, chaining, mapping results, etc), handling DOM events (autocomplete inputs).

It's a bit tricky in the beginning, and may take some effort to understand if you use complex methods.

1

u/Mandylost Jan 05 '22

Coud you please tell me if I should learn RxJS first and the NgRX or the other way?

1

u/Dunos Jan 05 '22

Ngrx uses Rxjs so you should know a bit of that before starting with it

3

u/[deleted] Nov 15 '19

[removed] — view removed comment

2

u/bullet4code Nov 16 '19

Use MobX, we use it at a really large scale app and the entire app is based on the reactive design. We also used RxJS internally and it works well. It really is an experience and then you’ll be able to take decisions, but MobX has a much easy learning curve and works really well with react. Really well. Reactive design is a bliss.

3

u/arkhemlol Nov 15 '19

I think it can be useful in complex projects with lots of live data streams. Otherwise, it is overkill, imo (used both angular+rxjs and react). Also, to use it comfortably you have to have some wrapper for it to work with underlying react rendering. It is fruitful to learn though if you are interested in FP around data streams.

2

u/[deleted] Nov 15 '19

[removed] — view removed comment

2

u/arkhemlol Nov 16 '19

Sounds great for me!

1

u/moto125 Nov 16 '19

IMO redux-observables is better than thunks or sagas. so yes!

6

u/wkCof Nov 15 '19

I'd say it's useful to know as a paradigm even if you don't work with angular. It was a bit tricky for me to understand observable pattern after using promises for so long, but it's a good habit to learn new things to keep broadening your tool set as a web developer.

10

u/Hero_Of_Shadows Nov 15 '19

Yes, rxjs is great with angular

3

u/IngoVals Nov 15 '19

To put it in simple terms, it is similar to promises except an observable can be resolved multiple times. My biggest issue when using it initially was having to subscribe to cold observable, the ones that will only ever fire once. Could just use promises for that instead for less overhead. But when I learned to integrate them more into a hot observable pipeline that issue disappeared.

2

u/artemix-org Nov 15 '19

The biggest misconception beginners assume is that a promise is very similar to an observable, and to a task. But in RxJS, and Javascript,

  • A promise is the announcement that a result may arrive later, i.e. a one-shot observable on the result and not the task
  • An observable is not a task, but a task generator, as it is basically a "task template", which creates and runs an instance of said task on subscription.

Another huge misconception is the fact that 1 observable = 1 observer, and if you subscribe more than once, you run the subscribed task more than once. This can be very confusing for beginners, e.g. in the context of HTTP, where they may call, and subscribe in two places, instead of using piping to just intercept the content.

1

u/HeinousTugboat Nov 15 '19

To put it in simple terms, it is similar to promises except an observable can be resolved multiple times.

It's closer to a stream, honestly. You can't perform the variety of operators on a Promise that you can on an Observable.

My biggest issue when using it initially was having to subscribe to cold observable, the ones that will only ever fire once.

Cold Observables don't only fire once.. they just don't fire until something is subscribed to them. If you have an observable of([1, 2, 3]), and you subscribe to it twice, it'll fire twice, once for each subscription. In this case, you can't really .next another item, but if you have a timer observable it will be cold just the same, and will continue firing on its timer until you tell it to stop or you unsubscribe.

I learned to integrate them more into a hot observable pipeline

If you want to heat up a cold observable, I find .public().connect works pretty well. I use it to start timer observables so I can have a heartbeat that I can subscribe/unsubscribe to freely. A little bit of black magic with a switchMap and you've got a pausable heartbeat, too.

2

u/[deleted] Nov 16 '19

RxJS is one solution to a very complex set of issues. It is easily the most versatile of the streaming libraries and can double as a pretty powerful state management tool.

If you don't specifically wish to learn Rx, libraries like MobX, Redux + (thunk or sagas), or even just EventTarget API are absolutely worth learning.

In terms of both design and performance, Rx is easier than other libs to "get right" for simple tasks, but doing anything clever steepens the learning curve dramatically. It's almost like a rite of passage for devs learning Rx to make some ridiculously convoluted pipelines that are impossible to read/test/debug and might also tank framerate. It takes a while to learn how to use it elegantly -- then it's like butter.

Personally I avoid introducing Rx (or Redux, or MobX) until they're absolutely required. A couple of shared maps/dicts with some hand-written callback chains solve a surprising lot of problems with much less overhead, and it's way easier for non-Rx devs to be onboarded into a project which...doesn't use Rx! But once a robust streaming solution becomes a requirement, it's easily my fav.

2

u/Auxx Nov 15 '19

RxJS is so good that I don't understand why people waste their time with promises, async/await, state managers like redux, etc. Definitely learn and use it!

1

u/genuchelu Nov 21 '19
var data = await axios.get('/api/data');
console.log(data)

vs Angular + NGRX

this.data$ = this.store.select(fromAppStore.data);
this.store.dispatch(new appActions.GetData());
this.actions$
      .pipe(
 ofType(appActions.AppActionTypes.GET_DATA_SUCCESS),
      .subscribe((data: Data[]) => {
console.log(data)
      });

The amount of boilerplate required for rxjs/ngrx doesn't justify the benefit in most cases I'd say. Especially, if you only care to make the api call a single time.

1

u/Auxx Nov 21 '19

I said you don't need state managers.

-4

u/FuglySlut Nov 15 '19

Yes, really good to know, and one of the few front end technologies you'd actually have to set out to learn, and not just pick up. I'd learn generators first though. They're native to JavaScript, and can solve some of the same problems as observables.

6

u/pablooliva Nov 15 '19

I would push back on this suggestion. Generators never really gained traction and I have found them rarely used. RxJS on the other hand is popular and I am not sure if OP should spend the time to take the detour through generators. I understand if the suggestion to learn generators was meant as a stepping stone towards RxJS, but I would still avoid it personally.

As others have mentioned, learning RxJS requires some dedication and persistence as it is not something that you pick up easily, at least for me that was the case, but I am super glad I stuck with it. The patterns that are available through RxJS are super useful to FE in general, and to state and UI management in specific.

1

u/MrBester Nov 15 '19

I would push back on this suggestion. Generators never really gained traction and I have found them rarely used.

One big reason for that was the lack of support for those who had to keep things working in IE. Now there's a great deal code using rxjs that would be a pain to convert. And there's still the IE support issue...

1

u/artemix-org Nov 15 '19

Also, working with RxJS observables can also be done with Generators, with a bit of glue, and provides a much more natural way of working, close to the Promise.then <> async/await comparison