3
votes

What the difference between Events in Ionic 3 and BehaviorSubject in Angular "Rxjs" in term of subscribing to event emit, for example, "When some event happens, emit it globally"?

2

2 Answers

2
votes

There are some few differences between then, but there's 3 i think it's worth focusing here.

BehaviourSubject emits his "event" when you subscribe to it.

So if you create a new BehaviourSubject instance and subscribe it'll execute the code inside of it even if you haven't used next(), like this:

var subject = new Rx.BehaviorSubject(0); // 0 is the initial value

subject.subscribe((value) => {
  // this code will fire when you subscribe to this new subject, then'll fire again for every next(value) you call
  console.log('observerA: ' + value);
});

Events'll only fire when you use is .publish() method.

BehaviourSubject is not global.

Since you need to create a new instance of it in a component and subscribe to it, it'll work only in this component. With Events you can always subscribe and publish from any component, since the first argument when publishing and event is a identifier any subscription to this identifier will fire when you publish.

BehaviourSubject stores the last value passed to it.

At any time you can use getValue() method of your subject to get the last value passed to it. With Events it's not possible, but you can create a variable and everytime you publish an event this variable'll hold the passed value.

Hope this helps.

2
votes

To understand BehaviorSubject, it's necessary to take time to digest how Subjects work and how they differ from an ordinary Observable.

Per Ben Lesh (a major contributor on Rxjs): "a more important distinction between Subject and Observable is that a Subject has state, it keeps a list of observers."

So, a Subject retains state. For further reading, I encourage you to read the distinction between "Hot" observables and "Cold" observables.

A BehaviorSubject differs from a Subject in that any consumer that subscribes to it will have the latest value of the BehaviorSubject immediately emitted to it.

So, say we have an app that has a BehaviorSubject that emits the username of the current user. Say our HomePage component is subscribed to this BehaviorSubject. As soon as that subscription is initialised (in HomePage) the username value will be emitted to it. Time goes by and still the same user is logged in. Say DetailsPage only now has subscribed to this same BehaviorSubject. The username has not changed during this time, but regardless, when this new subscription is initialised, the value will be emitted.

As far as I can tell an Event (in Ionic) is used to subscribe to and broadcast global events. A BehaviorSubject can be of any scope, not usually global.

I would say that a Subject is a closer comparison to an Event. A Subject can have a value pushed to it (like Event has publish) and it can also be subscribed to just like an Event.

Although again, a Subject is not typically constrained to any scope. That is, it's not designed to always be used across the global scope of an app.