What is rx?
The 'rx' npm package is a library for composing asynchronous and event-based programs using observable sequences. It provides powerful utilities for creating, transforming, and querying data streams.
What are rx's main functionalities?
Creating Observables
This code demonstrates how to create an Observable that emits a single value, 'Hello', and then completes.
const { Observable } = require('rx');
const observable = Observable.create(observer => {
observer.onNext('Hello');
observer.onCompleted();
});
observable.subscribe(value => console.log(value));
Transforming Data
This code shows how to transform data in an Observable sequence using the 'map' operator, multiplying each number by 10.
const { Observable } = require('rx');
const source = Observable.from([1, 2, 3]);
const multiplied = source.map(value => value * 10);
multiplied.subscribe(value => console.log(value));
Combining Observables
This code snippet illustrates how to combine two Observables into one using 'combineLatest', which emits values from both Observables as an array.
const { Observable } = require('rx');
const obs1 = Observable.of('Hello');
const obs2 = Observable.of('World');
const combined = Observable.combineLatest(obs1, obs2, (v1, v2) => v1 + ' ' + v2);
combined.subscribe(value => console.log(value));
Error Handling
This example demonstrates how to handle errors in an Observable sequence using the 'throw' operator and the error handling function in 'subscribe'.
const { Observable } = require('rx');
const source = Observable.throw(new Error('Oops!'));
source.subscribe(
value => console.log(value),
error => console.error(error.message)
);
Filtering Data
This code sample shows how to filter data in an Observable sequence using the 'filter' operator, emitting only even numbers.
const { Observable } = require('rx');
const source = Observable.from([1, 2, 3, 4, 5]);
const filtered = source.filter(value => value % 2 === 0);
filtered.subscribe(value => console.log(value));
Other packages similar to rx
rxjs
RxJS is a reactive programming library for JavaScript. It offers a more modern API and is more actively maintained compared to 'rx'. It is the standard choice for use with frameworks like Angular.
most
Most.js is another reactive programming library that focuses on high performance and low memory usage. It claims to be one of the fastest reactive streaming libraries.
baconjs
Bacon.js provides functional reactive programming and streams. It has a different API design and is known for its ease of use and integration with other libraries and frameworks.
kefir
Kefir.js is a Reactive Programming library with focus on high performance and low memory usage. It is similar to Bacon.js but with a smaller API surface and less overhead.
RxJS - Reactive Extensions for JavaScript
The Reactive Extensions for JavaScript (RxJS) are a set of libraries for composing and coordinating asynchronous and event-based programming that works across many JavaScript runtimes including all browsers and Node.js.
This set of libraries include:
- rx.js - Core library
- rx.aggregates.js - aggregation event processing query operations
- rx.binding.js - binding operators including multicast, publish, publishLast, publishValue, and replay
- rx.coincidence.js - reactive coincidence join event processing query operations
- rx.experimental.js - experimental operators including imperative operators and forkJoin
- rx.joinpatterns.js - join patterns event processing query operations
- rx.testing.js - used to write unit tests for complex event processing queries.
- rx.time.js - time-based event processing query operations.
Installation and Usage
There are multiple ways of getting started with the Reactive Extensions including:
In a Browser:
<script src="rx.js><script>
Along with a number of our extras for RxJS:
<script src="rx.aggregates.js><script>
<script src="rx.binding.js><script>
<script src="rx.coincidencejs><script>
<script src="rx.experimental.js><script>
<script src="rx.joinpatterns.js><script>
<script src="rx.testing.js><script>
<script src="rx.time.js><script>
Installing via NPM:
npm install rxjs
npm install -g rxjs
Using in Node.js:
var Rx = require('rx');
Installing all of RxJS via NuGet:
Install-Package RxJS-All
Or install via NuGet individual packages:
Install-Package RxJS-Main
Install-Package RxJS-Aggregates
Install-Package RxJS-Binding
Install-Package RxJS-Coincidence
Install-Package RxJS-Experimental
Install-Package RxJS-JoinPatterns
Install-Package RxJS-Testing
Install-Package RxJS-Time
Using RxJS with an AMD loader such as Require.js
require({
'paths': {
'rx': 'path/to/rx.js'
}
},
['rx'], function(Rx) {
var obs = Rx.Observable.returnValue(42);
obs.subscribe(function (x) { console.log(x); });
});
License
Source files are licensed under the Microsoft Reference Source License (MS-RSL)
Minimized files are licensed under the Reactive Extensions for .NET and JavaScript License.