What is rxjs?
RxJS (Reactive Extensions for JavaScript) is a library for reactive programming using Observables, to make it easier to compose asynchronous or callback-based code. It provides an API for asynchronous programming with observable streams.
What are rxjs's main functionalities?
Creating Observables
This feature allows you to create an Observable stream of data. The code sample demonstrates how to create an Observable that emits several values over time.
const { Observable } = require('rxjs');
const observable = new Observable(subscriber => {
subscriber.next(1);
subscriber.next(2);
subscriber.next(3);
setTimeout(() => {
subscriber.next(4);
subscriber.complete();
}, 1000);
});
Subscribing to Observables
This feature allows you to subscribe to an Observable and react to the data being emitted. The code sample shows how to subscribe to an Observable created from an array.
const { from } = require('rxjs');
const observable = from([10, 20, 30]);
const subscription = observable.subscribe(x => console.log(x));
Operators
Operators are methods on the Observable type that allow for composing asynchronous operations in a declarative manner. The code sample demonstrates the use of 'filter' and 'map' operators to process data.
const { of } = require('rxjs');
const { map, filter } = require('rxjs/operators');
const nums = of(1, 2, 3, 4, 5);
const squaredNums = nums.pipe(
filter(n => n % 2 !== 0),
map(n => n * n)
);
squaredNums.subscribe(x => console.log(x));
Handling Errors
This feature allows you to handle errors in an Observable sequence. The code sample demonstrates how to catch and handle errors using the 'catchError' operator.
const { throwError, of } = require('rxjs');
const { catchError } = require('rxjs/operators');
const observable = throwError(new Error('Something went wrong!'));
const handled = observable.pipe(
catchError(error => of(`Caught: ${error.message}`))
);
handled.subscribe(x => console.log(x), err => console.error(err));
Combining Observables
This feature allows you to combine multiple Observables into one. The code sample demonstrates how to use 'combineLatest' to combine two streams of data.
const { combineLatest, of } = require('rxjs');
const temperature = of(70, 72, 76, 79, 75);
const humidity = of(40, 44, 49, 58, 55);
const combined = combineLatest([temperature, humidity]);
combined.subscribe(([temp, hum]) => console.log(`Temperature: ${temp} Humidity: ${hum}`));
Other packages similar to rxjs
most
Most.js is a high-performance reactive programming library. It offers similar functionality to RxJS but focuses on providing a smaller, faster, and more modular library.
xstream
XStream is a library for building asynchronous and event-based programs using observable streams. It is similar to RxJS but with a focus on simplicity and minimalism, offering a smaller set of operators.
kefir
Kefir.js is a Reactive Programming library with a focus on high performance and low memory usage. It is similar to RxJS but is more lightweight and has a slightly different API.
RxJS: Reactive Extensions For JavaScript
The Roadmap from RxJS 7 to 8
Curious what's next for RxJS? Follow along with Issue 6367.
RxJS 7
Reactive Extensions Library for JavaScript. This is a rewrite of Reactive-Extensions/RxJS and is the latest production-ready version of RxJS. This rewrite is meant to have better performance, better modularity, better debuggable call stacks, while staying mostly backwards compatible, with some breaking changes that reduce the API surface.
Apache 2.0 License
Versions In This Repository
- master - This is all of the current work, which is against v7 of RxJS right now
- 6.x - This is the branch for version 6.X
Most PRs should be made to master.
Important
By contributing or commenting on issues in this repository, whether you've read them or not, you're agreeing to the Contributor Code of Conduct. Much like traffic laws, ignorance doesn't grant you immunity.
Installation and Usage
ES6 via npm
npm install rxjs
It's recommended to pull in the Observable creation methods you need directly from 'rxjs'
as shown below with range
.
If you're using RxJS version 7.2 or above, you can pull in any operator you need from the same spot, 'rxjs'
.
import { range, filter, map } from 'rxjs';
range(1, 200)
.pipe(
filter(x => x % 2 === 1),
map(x => x + x)
)
.subscribe(x => console.log(x));
If you're using RxJS version below 7.2, you can pull in any operator you need from one spot, under 'rxjs/operators'
.
import { range } from 'rxjs';
import { filter, map } from 'rxjs/operators';
range(1, 200)
.pipe(
filter(x => x % 2 === 1),
map(x => x + x)
)
.subscribe(x => console.log(x));
CDN
For CDN, you can use unpkg:
https://unpkg.com/rxjs@^7/dist/bundles/rxjs.umd.min.js
The global namespace for rxjs is rxjs
:
const { range } = rxjs;
const { filter, map } = rxjs.operators;
range(1, 200)
.pipe(
filter(x => x % 2 === 1),
map(x => x + x)
)
.subscribe(x => console.log(x));
Goals
- Smaller overall bundles sizes
- Provide better performance than preceding versions of RxJS
- To model/follow the Observable Spec Proposal to the observable
- Provide more modular file structure in a variety of formats
- Provide more debuggable call stacks than preceding versions of RxJS
Building/Testing
npm run compile
build everythingnpm test
run testsnpm run dtslint
run dtslint tests
Adding documentation
We appreciate all contributions to the documentation of any type. All of the information needed to get the docs app up and running locally as well as how to contribute can be found in the documentation directory.