ReactiveX (also known as Reactive Extensions) is a software library originally created by
Microsoft
Microsoft Corporation is an American multinational technology corporation producing computer software, consumer electronics, personal computers, and related services headquartered at the Microsoft Redmond campus located in Redmond, Washing ...
that allows
imperative programming
In computer science, imperative programming is a programming paradigm of software that uses statements that change a program's state. In much the same way that the imperative mood in natural languages expresses commands, an imperative program c ...
languages to operate on sequences of data regardless of whether the data is
synchronous
Synchronization is the coordination of events to operate a system in unison. For example, the conductor of an orchestra keeps the orchestra synchronized or ''in time''. Systems that operate with all parts in synchrony are said to be synchronou ...
or
asynchronous. It provides a set of sequence operators that operate on each item in the sequence. It is an implementation of
reactive programming
In computing, reactive programming is a declarative programming paradigm concerned with data streams and the propagation of change. With this paradigm, it's possible to express static (e.g., arrays) or dynamic (e.g., event emitters) data streams ...
and provides a blueprint for the tools to be implemented in multiple
programming languages
A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language.
The description of a programming ...
.
Overview
ReactiveX is an
API
An application programming interface (API) is a way for two or more computer programs to communicate with each other. It is a type of software interface, offering a service to other pieces of software. A document or standard that describes how ...
for
asynchronous programming
Asynchrony, in computer programming, refers to the occurrence of events independent of the main control flow, program flow and ways to deal with such events. These may be "outside" events such as the arrival of Unix signal, signals, or actions ins ...
with observable streams.
Asynchronous programming allows programmers to call functions and then have the functions "callback" when they are done, usually by giving the function the address of another function to execute when it is done. Programs designed in this way often avoid the overhead of having many threads constantly starting and stopping.
Observable streams (i.e. streams that can be observed) in the context of Reactive Extensions are like event emitters that emit three events: next, error, and complete. An observable emits next events until it either emits an error event or a complete event. However, at that point it will not emit any more events, unless it is subscribed to again.
Motivation
For sequences of data, it combines the advantages of
iterator
In computer programming, an iterator is an object that enables a programmer to traverse a container, particularly lists. Various types of iterators are often provided via a container's interface. Though the interface and semantics of a given iterat ...
s with the flexibility of event-based asynchronous programming. It also works as a simple promise, eliminating the
pyramid of doom that results from multiple layers of callbacks.
Observables and observers
ReactiveX is a combination of ideas from the
observer
An observer is one who engages in observation or in watching an experiment.
Observer may also refer to:
Computer science and information theory
* In information theory, any system which receives information from an object
* State observer in co ...
and the
iterator
In computer programming, an iterator is an object that enables a programmer to traverse a container, particularly lists. Various types of iterators are often provided via a container's interface. Though the interface and semantics of a given iterat ...
patterns and from
functional programming
In computer science, functional programming is a programming paradigm where programs are constructed by Function application, applying and Function composition (computer science), composing Function (computer science), functions. It is a declar ...
.
An observer subscribes to an observable sequence. The sequence then sends the items to the observer one at a time, usually by calling the provided callback function. The observer handles each one before processing the next one. If many events come in asynchronously, they must be stored in a queue or dropped. In ReactiveX, an observer will never be called with an item out of order or (in a multi-threaded context) called before the callback has returned for the previous item. Asynchronous calls remain asynchronous and may be handled by returning an observable.
It is similar to the iterators pattern in that if a fatal error occurs, it notifies the observer separately (by calling a second function). When all the items have been sent, it completes (and notifies the observer by calling a third function). The Reactive Extensions API also borrows many of its operators from iterator operators in other programming languages.
Reactive Extensions is different from
functional reactive programming
Functional reactive programming (FRP) is a programming paradigm for reactive programming ( asynchronous dataflow programming) using the building blocks of functional programming (e.g. map, reduce, filter). FRP has been used for programming graphi ...
as the Introduction to Reactive Extensions explains:
It is sometimes called "functional reactive programming" but this is a misnomer. ReactiveX may be functional, and it may be reactive, but "functional reactive programming" is a different animal. One main point of difference is that functional reactive programming operates on values that change continuously over time, while ReactiveX operates on discrete values that are emitted over time. (See Conal Elliott's work for more-precise information on functional reactive programming.)
Reactive operators
An operator is a function that takes one observable (the source) as its first argument and returns another observable (the destination, or outer observable). Then for every item that the source observable emits, it will apply a function to that item, and then emit it on the destination Observable. It can even emit another Observable on the destination observable. This is called an inner observable.
An operator that emits inner observables can be followed by another operator that in some way combines the items emitted by all the inner observables and emits the item on its outer observable. Examples include:
*
switchAll
– subscribes to each new inner observable as soon as it is emitted and unsubscribes from the previous one.
*
mergeAll
– subscribes to all inner observables as they are emitted and outputs their values in whatever order it receives them.
*
concatAll
– subscribes to each inner observable in order and waits for it to complete before subscribing to the next observable.
Operators can be chained together to create complex data flows that filter events based on certain criteria. Multiple operators can be applied to the same observable.
Some of the operators that can be used in Reactive Extensions may be familiar to programmers who use functional programming language, such as map, reduce, group, and zip.
There are many other operators available in Reactive Extensions, though the operators available in a particular implementation for a programming language may vary.
Reactive operator examples
Here is an example of using the map and reduce operators. We create an observable from a list of numbers. The map operator will then multiply each number by two and return an observable. The reduce operator will then sum up all the numbers provided to it (the value of 0 is the starting point). Calling subscribe will register an ''observer'' that will observe the values from the observable produced by the chain of operators. With the subscribe method, we are able to pass in an error-handling function, called whenever an error is emitted in the observable, and a completion function when the observable has finished emitting items.
import from 'rxjs';
const source: Observable = of(1, 2, 3, 4, 5);
source
.pipe(
map((value) => value * 2),
reduce((sum, value) => sum + value, 0)
)
.subscribe();
The above example uses th
RxJS implementationof Reactive Extensions for the JavaScript programming language.
History
Reactive Extensions (Rx) was created by the Cloud Programmability Team at
Microsoft
Microsoft Corporation is an American multinational technology corporation producing computer software, consumer electronics, personal computers, and related services headquartered at the Microsoft Redmond campus located in Redmond, Washing ...
around 2011, as a byproduct of a larger effort called
Volta. It was originally intended to provide an abstraction for events across different tiers in an application to support tier splitting in Volta. The project's logo represents an electric eel, which is a reference to Volta. The extensions suffix in the name is a reference to the
Parallel Extensions
Parallel Extensions was the development name for a managed concurrency library developed by a collaboration between Microsoft Research and the CLR team at Microsoft. The library was released in version 4.0 of the .NET Framework. It is composed ...
technology which was invented around the same time; the two are considered complementary.
The initial implementation of Rx was for
.NET Framework
The .NET Framework (pronounced as "''dot net"'') is a proprietary software framework developed by Microsoft that runs primarily on Microsoft Windows. It was the predominant implementation of the Common Language Infrastructure (CLI) until bein ...
and was released on June 21, 2011. Later, the team started the implementation of Rx for other platforms, including
JavaScript
JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of Website, websites use JavaScript on the Client (computing), client side ...
and
C++
C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
. The technology was released as
open source
Open source is source code that is made freely available for possible modification and redistribution. Products include permission to use the source code, design documents, or content of the product. The open-source model is a decentralized sof ...
in late 2012, initially on
CodePlex
CodePlex was a forge website by Microsoft. While it was active, it allowed shared development of open-source software.
Its features included wiki pages, source control based on Mercurial, TFVC, Subversion or Git, discussion forums, issue trac ...
. Later, the code moved to
GitHub
GitHub, Inc. () is an Internet hosting service for software development and version control using Git. It provides the distributed version control of Git plus access control, bug tracking, software feature requests, task management, continuous ...
.
See also
*
Erik Meijer (computer scientist)
Erik Meijer (born 18 April 1963, Curaçao) is a Dutch computer scientist, entrepreneur, and tie-dye enthusiast. From 2000 to early 2013 he was a software architect for Microsoft where he headed the ''Cloud Programmability Team''. He then founded ...
References
External links
ReactiveX reactive extensions website with documentation and list of implementation
*
{{Microsoft FOSS
Programming tools
Articles with example JavaScript code
Free and open-source software
Microsoft free software
Software using the Apache license
Software using the MIT license
2011 software