Skip to content

Davidhanson90/RxJS

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status Built with Grunt NPM version Bitdeli Badge

The Need to go Reactive | About the Reactive Extensions | Batteries Included | Why RxJS? | Dive In! | Resources | Getting Started | What about my libraries? | Compatibility | Contributing | License

The Reactive Extensions for JavaScript (RxJS) 2.2...

...is a set of libraries to compose asynchronous and event-based programs using observable collections and Array#extras style composition in JavaScript

The project is actively developed by Microsoft Open Technologies, Inc., in collaboration with a community of open source developers.

This project is a mirror of the CodePlex repository.

The Need to go Reactive

Reactive Programming is a hot topic as of late, especially with such things as the Reactive Manifesto. Applications, especially on the web have changed over the years from being a simple static page, to DHTML with animations, to the Ajax revolution. Each time, we're adding more complexity, more data, and asynchronous behavior to our applications. How do we manage it all? How do we scale it? By moving towards "Reactive Architectures" which are event-driven, resilient and responsive. With the Reactive Extensions, you have all the tools you need to help build these systems.

About the Reactive Extensions

The Reactive Extensions for JavaScript (RxJS) is a set of libraries for composing asynchronous and event-based programs using observable sequences and fluent query operators that many of you already know by Array#extras in JavaScript. Using RxJS, developers represent asynchronous data streams with Observables, query asynchronous data streams using our many operators, and parameterize the concurrency in the asynchronous data streams using Schedulers. Simply put, RxJS = Observables + Operators + Schedulers.

Whether you are authoring a web-based application in JavaScript or a server-side application in Node.js, you have to deal with asynchronous and event-based programming as a matter of course. Although some patterns are emerging such as the Promise pattern, handling exceptions, cancellation, and synchronization is difficult and error-prone.

Using RxJS, you can represent multiple asynchronous data streams (that come from diverse sources, e.g., stock quote, tweets, computer events, web service requests, etc.), and subscribe to the event stream using the Observer object. The Observable notifies the subscribed Observer instance whenever an event occurs.

Because observable sequences are data streams, you can query them using standard query operators implemented by the Observable type. Thus you can filter, project, aggregate, compose and perform time-based operations on multiple events easily by using these our many operators. In addition, there are a number of other reactive stream specific operators that allow powerful queries to be written. Cancellation, exceptions, and synchronization are also handled gracefully by using the methods on the Observable object.

But the best news of all is that you already know how to program like this. Take for example the following JavaScript code, where we get some stock data and then manipulate and then iterate the results.

/* Get stock data somehow */
var source = getStockData();

source
    .filter(function (quote) { 
        return quote.price > 30; 
    })
    .map(function (quote) { 
        return quote.price;
    })
    .forEach(function (price) {
        console.log('Prices higher than $30: $' + price);
    });

Now what if this data were to come as some sort of event, for example a stream, such as as a WebSocket, then we could pretty much write the same query to iterate our data, with very litle change.

/* Get stock data somehow */
var source = getAsyncStockData();

var subscription = source
    .filter(function (quote) { 
        return quote.price > 30; 
    })
    .map(function (quote) { 
        return quote.price;
    })
    .subscribe(
        function (price) {
            console.log('Prices higher than $30: $' + price);
        },
        function (err) {
            console.log('Something went wrong: ' + err.message);
        });

/* When we're done */
subscription.dispose();

The only difference is that we can handle the errors inline with our subscription. And when we're no longer interested in receiving the data as it comes steraming in, we call dispose on our subscription.

Batteries Included

This set of libraries include:

  • rx.lite.js - lite version with event bindings, creation, time and standard query operators with a compat file for older browsers.
  • rx.js - core library for ES5 compliant browsers and runtimes plus compatibility for older browsers.
  • rx.aggregates.js - aggregation event processing query operations
  • rx.async.js - async operationrs such as events, callbacks and promises plus a compat file for older browsers.
  • 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