ReactiveX (Rx, also known as Reactive Extensions) is a software library originally created by
Microsoft
Microsoft Corporation is an American multinational corporation and technology company, technology conglomerate headquartered in Redmond, Washington. Founded in 1975, the company became influential in the History of personal computers#The ear ...
that allows
imperative programming
In computer science, imperative programming is a programming paradigm of software that uses Statement (computer science), statements that change a program's state (computer science), state. In much the same way that the imperative mood in natural ...
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 synchrono ...
or
asynchronous
Asynchrony is any dynamic far from synchronization. If and as parts of an asynchronous system become more synchronized, those parts or even the whole system can be said to be in sync.
Asynchrony or asynchronous may refer to:
Electronics and com ...
. 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, declarative programming paradigm concerned with Stream (computing), data streams and the propagation of change. With this paradigm, it is possible to express static (e.g., arrays) or ...
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.
Programming languages are described in terms of their syntax (form) and semantics (meaning), usually defined by a formal language. Languages usually provide features ...
.
Overview
ReactiveX is an
API
An application programming interface (API) is a connection between computers or between computer programs. It is a type of software interface, offering a service to other pieces of software. A document or standard that describes how to build ...
for
asynchronous programming
Asynchrony, in computer programming, refers to the occurrence of events independent of the main program flow and ways to deal with such events. These may be "outside" events such as the arrival of signals, or actions instigated by a program that t ...
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 progressively provides access to each item of a collection, in order.
A collection may provide multiple iterators via its interface that provide items in different orders, such as forwards ...
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:
Fiction
* ''Observer'' (novel), a 2023 science fiction novel by Robert Lanza and Nancy Kress
* ''Observer'' (video game), a cyberpunk horr ...
and the
iterator
In computer programming, an iterator is an object that progressively provides access to each item of a collection, in order.
A collection may provide multiple iterators via its interface that provide items in different orders, such as forwards ...
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 declarat ...
.
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 graph ...
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 corporation and technology company, technology conglomerate headquartered in Redmond, Washington. Founded in 1975, the company became influential in the History of personal computers#The ear ...
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 compos ...
technology which was invented around the same time; the two are considered complementary.
The initial implementation of Rx was for
.NET Framework 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 and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior.
Web browsers have ...
and
C++
C++ (, pronounced "C plus plus" and sometimes abbreviated as CPP or CXX) is a high-level, general-purpose programming language created by Danish computer scientist Bjarne Stroustrup. First released in 1985 as an extension of the C programmin ...
. 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 and view the source code, design documents, or content of the product. The open source model is a decentrali ...
in late 2012, initially on
CodePlex. Later, the code moved to
GitHub
GitHub () is a Proprietary software, proprietary developer platform that allows developers to create, store, manage, and share their code. It uses Git to provide distributed version control and GitHub itself provides access control, bug trackin ...
.
See also
*
Erik Meijer (computer scientist)
Erik Meijer (born 18 April 1963, Curaçao) is a Dutch computer scientist and entrepreneur. From 2000 to early 2013, he was a software architect for Microsoft where he headed the Cloud Programmability Team. He then founded Applied Duality Inc. in ...
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