RXJS- Part 1

RXJS- Part 1

RXJS stands for Reactive Extension for Javascript, which is used to work with asynchronous data streams and Observable. Data Stream basically means data that is continuously generated by different sources.

Data Stream can be created from many things like UI events, HTTP request, Memory/Cache etc.

Stream basically means A sequence of ongoing events ordered in time, which emits a value, an error and a complete signal
Stream

From Rxjs 5+ the github location of Rxjs is https://github.com/ReactiveX/rxjs

Here I will discuss Rxjs based on Angular application

Previously we face a confusion that from where should we import things from Rxjs, like when we import Observable then we face the problem of path, like should we import it from "rxjs" or "rxjs/observable". But these problem has been fixed in the latest Rxjs(5+)

Basically we use two main resource of Rxjs, they are

1) Creation method :- Methods which are used to create an Observables (e.g Subject, from,of, range, interval etc).

Now we import these from only "rxjs"

import { range } from 'rxjs';

2) Operators :- Method which are used to modify the data from the stream in an Observables (e.g map,filter,take,scan etc)

We import operators from "rxjs/operators"

import { map, filter, scan } from 'rxjs/operators';

A brief overview of RXJS

No alt text provided for this image

Let's take a simple example. In the above picture we can see a common thing which we all encounter in our daily life. But lets name those things based on software teminilogy

Let the petrol jar be the "API" which is emitting data based on some request, or based on some corn-service or may be due to some other action.

The drop of petrol represent the "Data stream" which are produced at different interval of time

The top of the funnel is Observable which is allowing all the Data stream to flow through it

The middle portion is the Pipe, which will filter the data based on our requirement

And the bottom of the funnel is the "Observer" which will receive the data

Basically all the operators inside pipe will be in effect when we will subscribe it, until then it will not be activated

Let's take a simple example, where we will take number in some interval (will take upto 4 number) and then multiply the number with two


import { interval } from 'rxjs';
import { take,map } from 'rxjs/operators';
 
const numbers = interval(1000);
 
const takeFourNumbers = numbers.pipe(
  take(4),
  map(x => x*2)
);


takeFourNumbers.subscribe((data) => {
   console.log(data);   //0,2,4,6
  
})

Here as we discussed interval is an Creation method which will create a Observable hence imported from "rxjs" and take and map are operators.

We are creating a data stream of ascending numbers, one every second (1000ms)

const numbers = interval(1000);

Then as we know we must apply pipe to include the operators hence we are applying two operators, take(4) which will take only 4 values (0,1,2,3) and the map() which will convert the value (here doubling the value)

Since the operators will not work until and unless we apply subscribe() to it.

takeFourNumbers.subscribe((data) => {
    console.log(data)
  
})


Hope the article has helped you to gain some knowledge on RXJS. I will try to post an article on the next part. Please let me know is this helpful or not or if there is any way it can be improved it.

Bharat D

Servicenow DEVOPS Engineer at DHL

5 年

Bro please do important operator u must know in rxjs with practical example like petrol jar explanation is awesome to I understood pipe role please continue

要查看或添加评论,请登录

Souradip panja的更多文章

  • Frontend Security - CSP

    Frontend Security - CSP

    INTRODUCTION Content Security Policy (CSP) is an extra layer of security against attacks such as cross-site scripting…

  • Render Blocking and Prevention

    Render Blocking and Prevention

    In our daily web development, we have many faced some delay in rendering UI in the browser. This is generally known as…

    1 条评论
  • Optimizing Angular UI

    Optimizing Angular UI

    Sometime building website using Angular, cause some performance issue because of the bundle size. So for creating an…

  • BASIC INTRODUCTION OF BABEL

    BASIC INTRODUCTION OF BABEL

    Babel is a compiler which basically solve the problem of browser compatibility of the script. The problem that every…

  • Angular Performance Optimization - using pipes

    Angular Performance Optimization - using pipes

    In many of our applications we call a method from html, using interpolation.

    {{someMethod(paramaters)}}

  • Introduction to DOM-Part 1

    Introduction to DOM-Part 1

    In this article we will know about DOM. This is the first part of the article.

  • Django basics for Angular developers

    Django basics for Angular developers

    Hello friends, In this article we will look into the similarities of structure of Angular and Django. Any Angular…

  • Node js: Microservice ServiceRegister

    Node js: Microservice ServiceRegister

    Most microservice-based architectures are in constant in change phase, so we need to know the changes. This is what…

  • Code Optimization in Angular

    Code Optimization in Angular

    We all know, that code optimization is one of the biggest challenge we face in a large Enterprise level application…

  • Frontend Architechtures

    Frontend Architechtures

    Over the decade there has been many architectural change in the Application system design. Previously it's only…

    1 条评论

社区洞察

其他会员也浏览了