2

Since I'm working on angular / firebase, I'm a bit confused of the good use of Rxjs with firebase on Angular 2+ more particularly with Observable/Subject with firebase.on/once. I didn't found good examples of that.

First of all I think my understand of each one is ok (where I'm confused is when combining them), not sure about my explanations :

  • Subject: a data producer and a data consumer, observables who subscribe to this subject will be like "dependent"
  • Observable: a data producer where datas will be more "independent"
  • firebase.database().ref(...).on...: query called every time data change
  • firebase.database().ref(...).once...: query called once

Some questions/examples of what I saw (that confused me):

  • Using "firebase.database().ref(..).once..." (or .on) with Observable: What's the point of using Observable here if query already give us what we need ?
  • When is it better to use Subject over Observable + setters to update "myself" datas ?
  • Imagine you have /books and each book have some attributs (id, author, pages, title...) (in your code it will be stored in Book[]) and you want to update for example author, is it better to update just this attribute or all books with firebase (firebase.database().ref('/books').set(this.books);) ? I don't know why but I feel more secure just updating attribute than all objects but I often see the update with all objects, what's the best method ?
  • (Edit) What's the point of using firebase.database().on... with Subjects ? once will give the same result and will be more optimal (less queries) no ?
Emeric
  • 6,315
  • 2
  • 41
  • 54
  • You should consider using [angularfire2](https://github.com/angular/angularfire2) as it makes everything working with Observables for you, so you don't have to worry about al this. Basically, a `get` call will return an observable, that will emit a new value everytime the reference you called `get` on is updated. – Supamiu Aug 09 '18 at 07:04
  • @Supamiu I already tried angularfire but had some problems when typing my objects and...that doesn't solve my confusions (I think it's a good thing to understand concepts before using "tools") but you are right I should persist and train myself with angularfire ! – Emeric Aug 09 '18 at 07:12

1 Answers1

1

I'm going to answer your questions one by one:

What's the point of using Observables if query already gives us what we need?

I think the most important thing to note is that Observables are cold, meaning that you can create complex data flows, they wont be executed until someone subscribes to this flow, unlike query, where the query is executed right when you create it.

Observables also allow for query associations, dependencies between queries (get friend of a friend for instance), Also, Observables offer a simple way to listen data changes and repeat the same behavior on each change.

When is it better to use Subject over Observable + setters to update "myself" datas ?

The answer for this one is simple: always. Because firebase is realtime, you should do everything following this, have everything async. Also, Subject extends Observable, a Subject is basically an Observable that exposes its emit method, allowing you to emit custom data on it.

What's the best method to update a property inside an object?

For this one, I would totally encourage you to use firestore over firebase, for a simple reason: pricing (also because of data model tbh). Firestore is a document database, and your use case is totally meant for a document database. It's way cheaper than firebase as it scales on query amount, not data transfered like firebase does.

Also, udpating the whole object is simpler in terms of implementation, and requires less queries (if you're using firestore, you have to update the whole object, as it's not a giant object database like firebase), but it has a simple flaw: if user A updates a given book author name and user B updates the same book but only title, then using the same object might result in the last query overwriting the other one, but that's a very rare race condition and I'm not sure you should consider it over the cost of updating fields one by one.

Community
  • 1
  • 1
Supamiu
  • 8,501
  • 7
  • 42
  • 76
  • Thank's for your answer. I thought Firestore was only for android but after some search (https://stackoverflow.com/questions/46549766/whats-the-difference-between-cloud-firestore-and-the-firebase-realtime-database) it looks interesting (in my case my web app has a lot of chats = a lot of updates so I'm not sure firestore could be better) and I'll certainly take firestore in the future ! After reading your answer, I think I'll upgrade my entire project with Subjects ! I forgot to ask: what's the point of using firebase.database().on... with Subjects ? once will give the same result no ? – Emeric Aug 09 '18 at 12:00
  • @Curse the point is that using `.on` will create a request immediatly, not upon subscription, and you'll want to connect it inside Observables in the end. But again, angularfire2 handles everything for you, I'm creating an angular application with both firebase and firestore and I do everything easily thanks to angularfire2 :) – Supamiu Aug 09 '18 at 17:39
  • Thank's. Angularfire2 looks indeed really easier to use (+ no need to use service in most case you get data directly in component ?) but as I said I already tried it and I had a lot of problems when typing my objects, I needed to save them as "any" type or I had errors (and I didn't think that's was a good thing to save all my objects as any type) – Emeric Aug 09 '18 at 18:09
  • @Curse maybe this can help you: https://github.com/Supamiu/ffxiv-teamcraft/tree/staging/src/app/core/database/storage/firebase it's not perfect at all and I'm refactoring it but you might be able to get some help from it – Supamiu Aug 09 '18 at 18:13