DEV Community

Discussion on: Node Observer Pattern

Collapse
 
alemagio profile image
Alessandro Magionami

I believe, correct me if I'm wrong, that the main difference between the two patterns is:

  • observer, the subject is aware of its subscribers (observers) and can it can notify specifically to its subscribers
  • pub-sub, the publisher simply notifies its events and anyone can listen to those events

So, yeah, you are right, the pattern I described is, in fact, a pub-sub.

But I called it observer for 2 reasons:

  • the book i follow called it observer so i wanted to use the same title of the chapter to allow anyone to follow the series together with the book
  • in my opinion (I may change my mind in the future, who knows?) in Javascript, as you said, we have the event loop so, basically, it feels natural to solve the problem I described using such a pattern

I don't think in Node there is a case where you would use the "real" Observer pattern. But if you have one I'm open to discussion.

Collapse
 
shri3k profile image
shri3k

Yes, it's a bit tricky which is why I'm not married to any term.
I'm not sure if you've used redux but I actually think that's a good observer pattern, meaning any redux state change will trigger any subscribers that are subscribed to the subject.

You're right that Node in itself doesn't have any observer pattern to it like EventEmitter but you can make up one if needed.