Hello RxJS 5

In this post we are going to take a look at a very simple example for RxJS 5.

First download the starter project that has a server and Webpack setup:

https://github.com/aminmeyghani/es6-boiler

After you cloned/downloaded the starter project, follow the instructions to set it up.

Now, you have all you need to get started! First, we need to install RxJS. Run the following in the root of the project to install it:

npm i rxjs -S  

After that's done, in the client/main.js file, import Observable:

import {Observable} from 'rxjs/Rx';  

Now you have access to the Observable to work with. Let's create a stream with a single value:

const simpleStream = Observable.of('hello world');  

After creating the steam, you can subscribe or listen to it and pull the value when it is emitted:

simpleStream.subscribe((value) => {  
   console.log(value);
});

Note that the first function is called when the next value is emitted, but you can pass two other functions as well:

simpleStream.subscribe(  
  (value) => { console.log(value); },
  (err) => { console.log(err); },
  () => { console.log('done'); }
);

The second callback is called when there is an error, and the last callback is called when all the values have been emitted. To see the result, head over to http://localhost:8052/client/index.html and open the browser console to see the logs.