There are lots of different concepts in programming related to reacting when special occurrences happen. This post is an attempt at sorting these concepts out and illuminating how they relate to each other. Of course, a lot of this is based on my understanding, and it’s possible that different people will have different definitions of these terms; however, this is meant to accurately reflect the standard meanings.
Before we begin, note that these are simplified explanations of the concepts, and they don’t include everything that is useful or necessary to begin using them in code. They are explained with a slant towards distinguishing them from and relating them to each other.
Also, this article requires some familiarity with programming.
The List
Polling: Continually query whether something has occurred, and once the query returns true, do a reaction.
Event-driven: Provide the reaction to someone who will track whether something has occurred, and they will execute the reaction then. That someone is called the producer. We’re called the consumer. The reaction is specified as a function which has executable code. The function takes in an object called an event, which will contain the details of the occurrence. (For example, if the occurrence is a mouse click, then maybe the event object contains details about whether it was a left mouse or right mouse click.)
With event-driven, we can also have occurrences which we expect to repeat, where we want to react a common way each time.
Promise: With event-driven, the reaction only will execute once we’ve given it to the producer. If the occurrence has already taken place before we talked to the producer, nothing will happen. With a promise, we provide the reaction to someone who will make sure it gets executed after something has occurred, whether it occurred before the promise was made or not.
(For example, in web programming, we can have webpage code that says “once this button has loaded, do this.” However, if we used just event-driven and that code ran after the button already loaded, then the reaction wouldn’t happen. So we would use a promise instead.)
Pubsub: With event-driven, there is one producer who knows about the occurrences that we want to react to. However, sometimes not one producer will know everything, and so multiple people will need to be consulted in order to know when the occurrences take place. We might also want to have multiple people reacting to occurrences (in addition to us.) To do this, we have a channel that everyone interacts with. The people who track when the occurrences happen are called publishers. The people who want to react to the occurrences are called subscribers. When a publisher knows about an occurrence, they use the channel interface to publish an object called a message, that gives the details of the occurrence (just like an event does.) The channel interface then provides a mechanism for a subscriber to know when new messages are available to react to. This channel interface mechanism for a subscriber may be polling (query the channel for new messages) or event-driven (provide the channel with your reaction.)
ReactiveX/Observable: In this framework, one-time events, sequences of multiple-time events, and promises – anything that we can specify reaction functions for – are modelled as objects called observables. We can then perform transformations on observables, producing new observables. For example, we can model a sequence of mouse click events as an observable, then we can transform this observable into a new one that only models the events that corresponded to right mouse clicks. We can then react to this new observable, so we automatically filter out left-click mouse events. The functionality of ReactiveX/Observable is largely with these transformation functions.
React: This is the name of a specific product, a specific web client-side framework developed by Facebook. It’s so named because with it, you specify components of your webpage as templates to be filled in with values, and when the values change, you can call a special React method and the components will automatically update accordingly. (They will automatically “react.”)
Reactive programming: I have seen multiple common definitions for this, so my strategy is to just avoid using this term and use other words that are less ambiguous.
Written January 31, 2021.
