<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Ivan Trujillo Ayala</title>
    <description>The latest articles on Forem by Ivan Trujillo Ayala (@elkrival).</description>
    <link>https://forem.com/elkrival</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F799795%2Fd0b20278-3742-498d-bd5c-663809007380.jpeg</url>
      <title>Forem: Ivan Trujillo Ayala</title>
      <link>https://forem.com/elkrival</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/elkrival"/>
    <language>en</language>
    <item>
      <title>Understanding Observables in RxJS</title>
      <dc:creator>Ivan Trujillo Ayala</dc:creator>
      <pubDate>Wed, 19 Jan 2022 22:53:48 +0000</pubDate>
      <link>https://forem.com/thegnarco/understanding-observables-in-rxjs-40ma</link>
      <guid>https://forem.com/thegnarco/understanding-observables-in-rxjs-40ma</guid>
      <description>&lt;p&gt;Do you want a different way to handle state in your Javascript applications? Then I suggest giving Reactive Programming a try, and more specifically, &lt;a href="https://RxJS.dev/"&gt;RxJS&lt;/a&gt;. RxJS is a framework-agnostic library that provides tools for applying the Observer pattern. RxJS has extensive operators for handling state, and it is equipped with asynchronous event management functions. This means that no additional libraries are required to handle the asynchronous events for your application, which leads to a leaner dependency tree.&lt;/p&gt;

&lt;h2&gt;What is the &lt;a href="https://en.wikipedia.org/wiki/Observer_pattern"&gt;Observer Pattern&lt;/a&gt;?&lt;/h2&gt;

&lt;p&gt;The Observer pattern is a sequence of the following events:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;New data is emitted or state has changed in an &lt;code&gt;Observable&lt;/code&gt; object.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;Observer&lt;/code&gt;s of the &lt;code&gt;Observable&lt;/code&gt; are notified of the change.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;Subscribing to events&lt;/h2&gt;

&lt;p&gt;In RxJS all &lt;code&gt;Observables&lt;/code&gt; have an internal &lt;code&gt;subscribe&lt;/code&gt; function, this function is how different &lt;code&gt;Observers&lt;/code&gt; throughout the application are notified of &lt;code&gt;Observables&lt;/code&gt; updates.&lt;/p&gt;

&lt;h2&gt;Potential Snags&lt;/h2&gt;

&lt;p&gt;Here are some considerations before using this library.&lt;/p&gt;

&lt;p&gt;RxJS requires time to learn, implement, and practice. Engineers, make sure that there is ample runway to get everyone in your team up to speed because the learning curve can get steep quickly.&lt;/p&gt;

&lt;p&gt;The documentation is dense, it will most likely require more than one read through to grasp the concepts required to use it. We would like to see the RxJS authors polish its wording to make it more user friendly.&lt;/p&gt;

&lt;p&gt;Below is a simple example to get started with &lt;code&gt;Observables&lt;/code&gt; using RxJS.&lt;/p&gt;

&lt;h2&gt;How to Create an RxJS &lt;code&gt;Observable&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Install RxJS in your project:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;npm install RxJS&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In our main.js file:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;import { of } from '`RxJS`'
import './subscriptions'

const clientList = ['clientOne', 'clientTwo', 'clientThree']
export const clientsObs = of(...clientList)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In our subscriptions.js file:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;import { clientObs } from './main.js'

clientsObs.subscribe(client =&amp;gt; `console.log`(client))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The result of our &lt;code&gt;console.log&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;clientOne
clientTwo
clientThree&lt;/code&gt;&lt;/pre&gt;

&lt;ol&gt;
&lt;li&gt;In your main.js file import &lt;code&gt;RxJS&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Create a list of clients.&lt;/li&gt;
&lt;li&gt;Convert the list of clients into an &lt;code&gt;Observable&lt;/code&gt; using the &lt;a href="https://RxJS.dev/api/index/function/of"&gt;&lt;code&gt;of&lt;/code&gt;&lt;/a&gt; function.&lt;/li&gt;
&lt;li&gt;Import the &lt;code&gt;clientObs&lt;/code&gt; variable to the subscription module, and connect to it using the &lt;code&gt;subscribe&lt;/code&gt; method.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By using the &lt;code&gt;subscribe&lt;/code&gt; function in our subscription file, you create an &lt;code&gt;Observer&lt;/code&gt; of the &lt;code&gt;clientObs&lt;/code&gt;. This &lt;code&gt;Observer&lt;/code&gt; will be notified of new events emitted from the &lt;code&gt;clientsObs&lt;/code&gt;, which is exported from the &lt;code&gt;main.js&lt;/code&gt; file. In this example our events print to the &lt;code&gt;console.log&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This pattern is useful because it allows us to write decoupled code, that is still aware of events and new data from other observed sources.&lt;/p&gt;

&lt;p&gt;In our example we have defined an &lt;code&gt;Observable&lt;/code&gt; from our &lt;code&gt;clientList&lt;/code&gt; named &lt;code&gt;clientsObs&lt;/code&gt;, we imported our &lt;code&gt;clientObs&lt;/code&gt; to our subscription file, in our subscription file we &lt;code&gt;subscribe&lt;/code&gt; to the &lt;code&gt;clientObs&lt;/code&gt;, this action of subscribing makes the subscription file an &lt;code&gt;Observer&lt;/code&gt;, then the &lt;code&gt;clientObs&lt;/code&gt; emitted three different events at different times, and our subscription &lt;code&gt;Observer&lt;/code&gt; printed those events to the console.&lt;/p&gt;

&lt;p&gt;Congratulations, we have written our first &lt;a href="https://RxJS.dev/guide/observable"&gt;&lt;code&gt;Observable&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;If you are considering using this library, just remember that RxJS has its snags in the form of time and dense documentation. On the other hand, RxJS allows us to write applications using the &lt;code&gt;Observer&lt;/code&gt; Pattern, which is powerful and useful, because out of the box we can write different &lt;code&gt;Observable&lt;/code&gt; sources anywhere in our project. RxJS works well with established libraries, plays nice with Typescript, and addresses scalability issues through it's many operators.&lt;/p&gt;

&lt;p&gt;Give RxJS and &lt;code&gt;Observables&lt;/code&gt; a try if you are interested in exploring another way to manage state in Javascript.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Learn more about how The Gnar &lt;a href="https://www.thegnar.com/software-development/react-development-js"&gt;builds React applications&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
