Course contentsShow
← JavaScript
Lesson 2111 of 2,72344. Reactive PatternsFree lesson

Push vs Pull Models

Compare passing data with notifications (push) versus observers querying the subject for data (pull).

Push vs Pull Models

You'll learn: the difference between a subject actively delivering data to observers (push) versus observers retrieving data on demand (pull).

Two Ways to Exchange Information

In the Observer pattern, there are two fundamental approaches to how observers get data from the subject they're watching:

Push Model: The subject sends complete data to observers when notifying them. When something changes, the subject packages up all relevant information and delivers it directly to each observer's update method.

Pull Model: The subject only sends a notification (or a reference to itself). Observers then query the subject for the specific data they need.

Analogy

Think of a newsletter subscription:

  • Push: The full article arrives in your inbox
  • Pull: You get a notification saying "new article available" — you visit the website to read it

When Each Wins

Push advantages:

  • Simple for observers (data arrives ready-to-use)
  • Efficient when observers need all the data

Pull advantages:

  • Observers request only what they need
  • Subject doesn't assume what observers care about
  • More flexible when observers have varied interests

Code Context

In your simple Subject implementation, you might push data:

notify(data) {
  this.observers.forEach(obs => obs.update(data));
}

Or just push a reference (pull model):

notify() {
  this.observers.forEach(obs => obs.update(this));
}
// Observer pulls: update(subject) { const data = subject.getData(); }

Key Takeaway: Push models deliver complete data with notifications; pull models let observers query the subject for exactly what they need, trading immediacy for flexibility.