RxJs fromEvent explained - element events as streams

fromEvent(element, eventName, options) => observable object

This method generates an observable steam from events firering on an element. You can then add subscriptions on this observable. Conceptually you can view it like this: The fromEvent gives you a water tap, out of which comes data in the form of events.

The subscription can in that perspective be viewed as a hose: when you attach the hose to the water tap you have a place where events streams into. The events are the parameters in you subscribe('event data') .

You can attach more hoses (the subscriptions ) to this water tap (the observable ), it is you can add more than one subscription to the observable object retured by fromEvent .

Observable

mouseMoveObservable$

Stream

Subscription

mouseMove.subscribe(({ clientX, clientY }) => {
...
// your code
...
})

Try to move mouse over this rectangle

The output from another subscription on the mouseMoveObservable$ observable stream

- -

Naming convension - postfix with $

It is a good practice to postfix the variable name of an observable stream with a $ . The stream in this pen on mousemove events is for instance named mouseMoveObservable$ .