Search⌘ K
AI Features

RxJS in Node.js

Explore how to run RxJS in Node.js to create reactive applications. Understand reactive programming concepts with RxJS Observables, how to install and use RxJS in Node.js, and how to manage asynchronous data streams effectively.

We'll cover the following...

What is reactive programming?

Reactive programming is a programming paradigm that encompasses many concepts and techniques. In this course, we’ll focus particularly on creating, transforming, and reacting to streams of data. Mouse clicks, network requests, arrays of strings—all these can be expressed as streams to which we can “react” as they publish new values, using the same interfaces regardless of their source.

Reactive programming focuses on propagating changes without us having to explicitly specify how the propagation happens. This allows us to state what our code should do, without having to code every step to do it. This results in a more reliable and maintainable approach to building software.

What is RxJS?

RxJS is a JavaScript implementation of Reactive Extensions or Rx. Rx is a reactive programming model originally created at Microsoft, which allows developers to easily compose asynchronous streams of data. It provides a common interface to combine and transform data from wildly different sources, such as filesystem operations, user interaction, and social-network updates.

Rx started with an implementation for .NET, but today it has a well-maintained, open-source implementation in every major language and also in some minor ones. It is becoming the standard to program reactive applications. Additionally, Rx’s main data type, the Observable, is being proposed for inclusion in ECMAScript 7 as an integral part of JavaScript.

Running RxJS code in Node.js

Running code examples in Node.js is easy. We just need to make sure that we install the RxJS dependency in our project using npm:

Shell
$ npm install rx

After that, we can import the RxJS library in your JavaScript files:

Javascript (babel-node)
const Rx = require('rx');
Javascript (babel-node)
var Rx = require('rx');
Rx.Observable.just('Hello World!').subscribe(function(value)
{
console.log(value);
});

And we can run it by simply invoking the node and name of the file:

Shell
$ node test.js

The JavaScript file will execute and show the output from the command shown above.