liveBook · Manning

Push model

As explained in The Joy of JavaScript

Push-based programming is a style where data is delivered to consumers when it becomes available rather than being pulled on demand.

In the push model, a producer decides when to send data. A consumer subscribes, then reacts as values arrive rather than repeatedly asking for them. A button click is a push event; calling a function or an iterator’s next method is pull. section 9.3.1

The distinction matters when choosing how to handle incoming values:

  • A Promise supplies one pushed value. A stream can deliver successive values as they become available. section 9.3.1
  • For incoming chat messages, periodic polling can produce awkwardly large responses. A push stream can notify the application of messages individually or in small batches. section 9.0

Make an array notify subscribers

An array normally exposes values for consumers to retrieve. A push-enabled array can instead use an EventEmitter: its push(value) method emits an event, and subscribe({ next(value) { … } }) registers a consumer for that event. The subscriber runs when a new value is pushed, without polling the array. section 9.3.2

An Observable gives this flow a reusable interface. It models a lazy push-based source: subscribing starts the flow, an observer receives values through next, and a subscription provides unsubscribe. The observer can also handle errors and completion. section 9.4.1

Add push behavior without changing the data structure

A Proxy can intercept an existing object’s push calls and emit each added value to observable subscribers. With a reactivized array, a subscriber can filter even numbers, square them, and receive the result when 6 is pushed. The observable also replays values already in the array when subscribed to. section 9.4.8

How The Joy of JavaScript covers it

How deep it goes
Defines push against pull, then works through EventEmitter subscriptions and a Proxy-based observable adapter.
Examples
Illustrates incoming chat messages, an EventEmitter-backed array, and a proxied array whose new values enter an observable pipeline.
What it assumes
Builds on JavaScript functions and arrays while reviewing iterator protocols and introducing the EventEmitter basics needed for subscriptions.
What you can do after
Build it from scratch with EventEmitter-backed subscriptions, or adapt a pushable iterable using Proxy.
How it connects
Connects push delivery to promises, streams, iterators, EventEmitter, observables, and subscriptions.

Also explained in

Compare how 4 books explain push model →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Joy of JavaScript. The section links point to the parts of the book it draws on.