<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="https://web-engineering.info"  xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
 <title>web-engineering.info - reactive programming</title>
 <link>https://web-engineering.info/taxonomy/term/49</link>
 <description></description>
 <language>en</language>
<item>
 <title>What Is Reactive Programming and how Does It Work?</title>
 <link>https://web-engineering.info/node/63</link>
 <description>&lt;div class=&quot;field field-name-body field-type-text-with-summary field-label-hidden view-mode-rss&quot;&gt;&lt;div class=&quot;field-items&quot;&gt;&lt;div class=&quot;field-item even&quot; property=&quot;content:encoded&quot;&gt;&lt;article&gt;
&lt;p&gt;The term &quot;reactive programming&quot; refers to a programming style based on handling &quot;events&quot; of all sorts. App developers are familiar with simple user interface events, such as mouse clicks, and know how to deal with them using their favorite programming language. But there are also other kinds of events an app may have to deal with, such as a state change of an object, an incoming message, the completion of an asynchronuous operation or an exception/error event.&lt;/p&gt;

&lt;p&gt;The goal of reactive programming is to provide a systematic way how to deal with all kinds of events in an app. This is supported by a number of relatively new libraries for various programming languages, including RxJS and Bacon.js for JavaScript, Rx.NET for C# and RxJava for Java.&lt;/p&gt;

&lt;p&gt;For understanding the potential of reactive programming, one has to understand the semantics of events and how they differ from objects.&lt;/p&gt;

&lt;p&gt;&lt;small&gt;In the following sections, we assume that the reader is familiar with basic concepts of object-oriented programming (objects, classes, properties, methods, class hierarchies, inheritance) and how they are visualized in UML class diagrams, and knows how to deal with objects in JavaScript.&lt;/small&gt;&lt;/p&gt;

&lt;section&gt;
&lt;h1&gt;Objects and Events&lt;/h1&gt;

&lt;p&gt;In the real world, we have to deal with objects (such as balls or cars) as well as with events (such as a goal in a soccer game or a car accident). The same holds for the digital world, where we deal with programming objects and programming events, which may represent real-world objects and real-world events. Digital events, such as a Twitter tweet, an incoming HTTP response message or a user interface event (like a mouse click), are a special kind of real-world events. They are typically represented in an app by corresponding programming events that can be &quot;caught&quot; and processed by the app.&lt;/p&gt;

&lt;p&gt;While &lt;strong&gt;&lt;em&gt;objects exist&lt;/em&gt;&lt;/strong&gt; in time, having a &lt;i&gt;life span&lt;/i&gt;, &lt;strong&gt;&lt;em&gt;events happen&lt;/em&gt;&lt;/strong&gt;, having an &lt;i&gt;occurrence time&lt;/i&gt; and possibly changing the state of affected objects. Therefore, in an object-oriented programming language like JavaScript, real-world objects are represented by persistent programming objects, which are stored in databases, while real-world events are represented by transient programming objects that are typically destroyed after being processed (events are also said to be &quot;consumed&quot;).&lt;/p&gt;

&lt;p&gt;There is a special connection between objects and events: &lt;strong&gt;&lt;em&gt;objects participate in events&lt;/em&gt;&lt;/strong&gt; by playing a specific role. This includes the case where an object is affected by an event in the sense that its state is changed through the event. For example, one or more cars participate in a car accident, and their state may be changed through the accident since they may be damaged. Also, a Twitter user may participate in a tweet as its user/author, as shown in the UML class diagram in Fig. 1, where both the object type &lt;code&gt;User&lt;/code&gt; and the event type &lt;code&gt;Tweet&lt;/code&gt; are modeled as classes.&lt;/p&gt;

&lt;figure&gt;&lt;img alt=&quot;&quot; src=&quot;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAANYAAAA6CAIAAABDBzCAAAAHZklEQVR4nO1dwZm0KhD028N/3iS4bwAmQQymYApkQAZEYAQkYAImQAIG0O9QuzWsOu7MiKPyqMN8KypLdxdNC9hWX19fVY74+vqSjJCxmaqqqo5W7y7ITK7MxCG+mXh0M3ZBZnJlJg5RKHgZZCYOUSh4GbwgTt/3Wuu4xBgzDEO6RiXAWymolPqzJCEKBfu+r+s6Lum67hEKTuwyp3JC/EFB7/3wAxEZhqFpGq01Stq2xWVt26KEZ1HYdZ3W2hgjItZapZQxZhxH3MWSSVUhBGMMxG6aBuWoiofOOf6uyLZBM6fDojghhK7rRAS/ItK2LXUOChpjtNa8LIQgM32KCGzXdd3cUm3b3rPUxMoiYq2dHPL3nlxrFKzrOoTAnoTDcRxREpcvnkVzm6ZxzvV9r5Tq+56VxyXzqnBv27aQHL2Q3bHrOmNM13XUyKJs905dEYviNE3jvceviGit0S2hN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CIqKUqqoKTyrw4fITbXAXApnKIAOqKbHgHGnFAdWgfPoFhE84RGgE03CDT8IGECkpOI7jZHsFD+flk72r3DWEwxILzrGHOBPNwzRUfgghttT2VftFlKnpyyAzcYhCwcsgM3GIQsHLIDNxiELByyAzcYiqqqp///5VOeLj4+PoJqREZuIQ3/Q7uifsgszkykwc4puJRzdjF2QmV2biEDcK8iUjAssyx7QrBTKzGcSZLB31e6baeA9uFGTWDiLJO0QHIksKTl7+ePllqPPgRkG+jsl3lQsFTwWIw+3A2KCJbSyHtmsrbhTkC+dgnvd+/ub5tZAlBbFQa63lLszcKDhPAXFo8zYhYwpOEp4c3LJtKBS8DP4vFJSfLX3Yz1woeB7MB2LkjsmHgpyUwQ4/a633PtXLbIcgSwqaKHUkRq0kCRIORJmavgwyE4coFLwMMhOHKBT8hTMHHhmbqVDwG9ZaRMC7Rlec0nsWGZupUPAbSBC961QU0mgclV/wnPhFwQfTYp7t2yn38KzNkAH8hUTkT+HAFJfnxI2Cj6vmKlPWL9jshSzkz6JQcIIbBeMXeJ1zWmt8i4Lp+pFkBFPWyJ98cNv/wjlttpGCeMOc75nDKIgfQgjYYoJDJBimpbCtCzsb0kq0EcteMP4jhIDPFvCzShl7wTdgIwXjjwXFDpvrq/zeBK4cx5Fregi02rY9lftYoCDEQI9BkiURiTMcFAq+DHRm9fvzWg8iXqCTyArIDog6Y37TXkylQge5JftecixQEHnZ44v4qAitFQq+DGaSeEGBixTkd3Xmu0zmFEwiQnLcKAjXjSVIRAzGGH6OK4SA2EJ+ZhYe//7HUTghBbdgkYKI2pHBbJ2CsCYe+U81p/FrUmaSBYbZRtji+OyZFxKALClIE9AotBosEpfjj/iWVPl6EqJMTV8GmYlDFApeBpmJQxQKXgaZiUMUCl4GmYlDVFVVfX5+Vjni8/PzaPWmRMZm+g96sFsysK4yCwAAAABJRU5ErkJggg==&quot; /&gt;
&lt;figcaption&gt;Figure 1: A Tweet event has a user (its author) as a participant.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;As we can see in the class diagram in Fig. 1, the object type &lt;code&gt;User&lt;/code&gt; has two properties: &lt;code&gt;id&lt;/code&gt; and &lt;code&gt;name&lt;/code&gt;, while the event type &lt;code&gt;Tweet&lt;/code&gt; has, in addition to the two properties &lt;code&gt;id&lt;/code&gt; and &lt;code&gt;text&lt;/code&gt;, also a reference property &lt;code&gt;user&lt;/code&gt;, which represents the association of a Twitter user as the author of a tweet.&lt;/p&gt;

&lt;p&gt;At runtime, an event is represented by a special programming object instantiating an event class representing its type. Like any other programming object, a programming event may have property value slots, like the Tweet event {id: 4711, user: {id: 6253282, name:&quot;realDonaldTrump&quot;}, text:&quot;...&quot;}, according to the properties defined by its type, possibly including the event&#039;s occurrence time and references to the objects participating in it.&lt;/p&gt;

&lt;p&gt;Events normally occur &quot;asynchronously&quot;, that is, we don&#039;t know their occurrence time in advance even when we expect them to occur soon, and we don&#039;t want to wait for them, but rather do some useful things meanwhile.&lt;/p&gt;
&lt;/section&gt;

&lt;section&gt;
&lt;h1&gt;Reacting to User Interface Events with Event Handlers&lt;/h1&gt;

&lt;p&gt;&lt;small&gt;In this section, we summarize how to deal with user interface events in JavaScript.&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;The most common type of events in an app are user interface events arising from user actions. In a web app, user interfaces are implemented with HTML, CSS and JavaScript. User actions, such as a mouse click on a button or text input in an input field, are represented as &quot;DOM events&quot; in the JavaScript environment of a browser (recall that the acronym DOM stands for &quot;Document Object Model&quot;, which is a standard that essentially defines a set of interfaces for dealing with HTML elements as JavaScript objects). For defining a variety of user interface event types, the DOM Events standard defines a hierarchy of interfaces that are implemented by corresponding JavaScript classes.&lt;/p&gt;

&lt;p&gt;An important type of DOM events are mouse events, which are targeted towards a specific HTML element (their &quot;target&quot;). As shown in Fig. 2, &lt;code&gt;click&lt;/code&gt; and &lt;code&gt;dblclick&lt;/code&gt; events are mouse events and mouse events are DOM events. This is the subclass/inheritance hierarchy expressed in the class diagram of Fig. 2 with arrows having a large arrowhead. It implies that both &lt;code&gt;click&lt;/code&gt; and &lt;code&gt;dblclick&lt;/code&gt; events inherit the screen coordinate properties &lt;code&gt;screenX&lt;/code&gt; and &lt;code&gt;screenY&lt;/code&gt; from the class &lt;code&gt;MouseEvent&lt;/code&gt;, and the &lt;code&gt;target&lt;/code&gt; property and&amp;nbsp;&lt;code&gt;preventDefault()&lt;/code&gt; method from &lt;code&gt;DOMEvent&lt;/code&gt;.&lt;/p&gt;

&lt;figure&gt;&lt;img alt=&quot;&quot; src=&quot;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAakAAABDCAIAAACp5m1cAAAWvklEQVR4nO1dMYzjRBcegbgKabdCR4FyBUZUq0WhoAjimnRIEMkSV8dCogGRUwqa6IpThOTmGisUiCYuqCLkAl26FCmuiYKoLq6g8pVbpLhy/uIj3/9unHhjx07srL8icsYz45mx/fm9N2/eqKurK3WOuLq60jVq1KixA0optTpHKKVOPbY1atQoL2ruq1EiXF9fn1JZOBaur69PPdI1au6rUSbckbt2R7pZctTcdwycqziTu/yiynTXisMd6WbJodSxuK/dbk+n0+SUHFGqx6tUjckRuffrXAfKwB3pZsmRxH3j8Xi6wWq1mk6ntm2DsKbTabfbRbZut4sUnkXiaDRqt9u9Xg9VNRqNbrc7Ho9ZOVPa7TYSR6PRaDTq9XqTyaTdbtu2jXSjcjQMvzX3nRBV5z7HcaIoSk4pAuf6PFQLSdzXbDbn83mz2ZR/l8slUmT61rMgR9u2XddFOg5QSqaQ1HDAst1uF9RpVD6fz9vtNn5r7jshjs996/V6vV7jACm+73ueh5QgCJAYBAEy8KzWOgzDMAx93/d9H/lbrZbv+6xKphilULPneShrXJrtYVUHdrPGEbCT+waDgeu6vV7Pdd3VajWZTBqNRq/X6/V6jUZjMpnYtj0ej8fjsW3b8bPNZnMymaxWKySCv5AiuRUprutCxCNvIp31GJWDFl3XxW/NfafC8bmv0+lEUYRfrTWoKggCpLRaLWTDgTyrtR4Oh0hxHGc4HM5mM8uy+v3+b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3Yzx0GrkQ05cF+j0bBtez6fS+7j8l4egMiwYhdrP8h9oMhGo+G67nw+hw8zMh8o/ZXkIStJM3JHQdwHOqNxTXqQGEFumILj5Jg3WmvMhpPppPkvXjxeil8gmG4hBmIeBulojO/7ctqds1jFjVuNDMiH+6C0xnVerksDzUld2OA+Qy+GDKiU2hWkYE+U5CErSTNyR3Hcl2+1J0GpfFxqxJEP93H1LrkPC9GazWa73Zbch5xSEZbch5W8y+USid1ut5b7yowiuO/4UQgLwmw2C8NQLnUgzvV5qBZy4D4EIGg2m1LnBQ8qpRDfBTTX7XZxShKiNO3hLBbtsiqGR625r2wogvvyrbCcuCPdLDnymedF0Co65TH2FH30pKorj1dvuvLxLBPpM5gNJXnIzniyLN+BUuW4X0XjjnSz5KjXddQoEe7I/boj3Sw5au6rUSKcq4BsoIg14zXSoua+GjVq3EXU3FejRo27iP+o4eLi4rRaQEG4uLg47fjWqFGjnPgfnyrYY+Ba1CUAAAAASUVORK5CYII=&quot; /&gt;
&lt;figcaption&gt;Figure 2: HTML elements participate as targets in mouse events.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Processing an event means to react to it. The most widely used standard API for reacting to events is the &lt;a href=&quot;https://dom.spec.whatwg.org/#events&quot;&gt;&quot;DOM Events&quot; API&lt;/a&gt;, which defines how to register event handlers (or &#039;callbacks&#039;) in the form of &lt;i&gt;event listeners&lt;/i&gt; and how to access event properties. An event handler is a procedure that defines a reaction to events of a certain type. For instance, we could define an event handler that shows the hidden content of a special web page section, containing an optional explanation, and that is triggered whenever the user clicks on the section heading. Here, the triggering event type would be &quot;click&quot; events on &lt;code&gt;h1&lt;/code&gt; elements within &lt;code&gt;section&lt;/code&gt; elements.&lt;/p&gt;

&lt;p&gt;An event listener associates an event handling procedure (&quot;event handler&quot;) with a DOM event type and an HTML element as the observer of events. Since the DOM defines a &quot;bubbling&quot; logic for catching events, we have to distinguish between the &lt;i&gt;target&lt;/i&gt; element, where the event occurs, and the &lt;i&gt;observer&lt;/i&gt; element, where the event is caught and processed.&lt;/p&gt;

&lt;p&gt;Event bubbling means that when an event occurs at some target element, the browser first executes any event listeners defined for this type of event at the target element. The event is then propagated upwards the HTML element hierarchy via all ancestor elements to the root element &lt;code&gt;html&lt;/code&gt;, and on its way further event listeners may be executed, if there are any. This mechanism allows to catch events either at the target element where they occur, or higher up in the HTML element hierarchy by defining an event listener at some ancestor element playing the role of an observer.&lt;/p&gt;

&lt;p&gt;The following JS function is an example of an event handler. It displays an event&#039;s type (&lt;code&gt;e.type&lt;/code&gt;), its target (&lt;code&gt;e.target&lt;/code&gt;), and its observer (&lt;code&gt;e.currentTarget&lt;/code&gt;) within a user interface element with ID &quot;message-panel&quot;:&lt;/p&gt;

&lt;pre&gt;
function handleEvent(&lt;strong&gt;e&lt;/strong&gt;) {
  msg = &quot;Event type: &quot; + &lt;strong&gt;e.type&lt;/strong&gt; + &quot;, &quot;;
  msg = msg + &quot;, target: &quot; + &lt;strong&gt;e.target&lt;/strong&gt;.nodeName;
  msg = msg + &quot;observer: &quot; + &lt;strong&gt;e.currentTarget&lt;/strong&gt;.nodeName;
  document.getElementById(&quot;message-panel&quot;).innerHTML += &quot;&amp;lt;p&amp;gt;&quot; + msg + &quot;&amp;lt;/p&amp;gt;&quot;;
}&lt;/pre&gt;

&lt;p&gt;Notice that the events, which trigger the execution of an event handler like &lt;code&gt;handleEvent(e)&lt;/code&gt;, are passed to the event handler as the value of its event parameter &lt;code&gt;e&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For defining event listeners with &lt;code&gt;handleEvent&lt;/code&gt; we can use a procedure &lt;code&gt;init&lt;/code&gt; that is executed immediately after loading the web page. In this procedure, we define three event listeners:&lt;/p&gt;

&lt;pre&gt;
function init() {
  var b = document.body;
  var t1 = document.getElementById(&quot;t1&quot;);
  b.addEventListener(&quot;click&quot;, handleEvent);
  b.addEventListener(&quot;dblclick&quot;, handleEvent);
  t1.addEventListener(&quot;click&quot;, handleEvent);
}
// execute the init function when document has been loaded
window.addEventListener(&quot;load&quot;, init);&lt;/pre&gt;

&lt;p&gt;With these event listeners we catch &lt;code&gt;click&lt;/code&gt; events at the &lt;code&gt;body&lt;/code&gt; element and at an element with ID &quot;t1&quot;, as well as &lt;code&gt;dblclick&lt;/code&gt; events at the &lt;code&gt;body&lt;/code&gt; element.&lt;/p&gt;
&lt;/section&gt;

&lt;section&gt;
&lt;h1&gt;Event Sources and Event Streams&lt;/h1&gt;

&lt;p&gt;In general, a program execution environment, like a web browser or NodeJS, defines a set of typed &lt;b&gt;&lt;i&gt;event sources&lt;/i&gt;&lt;/b&gt;. In the case of a web browser, they include various types of user interface events and events related to HTTP messages in the context of the XmlHttpRequest (XHR) API.&lt;/p&gt;

&lt;p&gt;Any event source can be considered as the basis of an &lt;b&gt;&lt;i&gt;event stream&lt;/i&gt;&lt;/b&gt;, which is simply the sequence of occurrences of events of the type defined by the event source. For instance, the Twitter tweets of Donald Trump define an event stream. Also the successive click events during a HTML page view session define an event stream. An event occurrence often comes with some event data. This is why event streams are sometimes also called &quot;asynchronous data streams&quot;. However, since the distinction between objects and events is helpful for understanding the semantics of reactive programming, we prefer the name &quot;event stream&quot; over &quot;asynchronous data stream&quot;.&lt;/p&gt;
&lt;/section&gt;

&lt;section&gt;
&lt;h1&gt;Reacting to Possibly Complex Events with Event Streams&lt;/h1&gt;

&lt;p&gt;The goal of reactive programming libraries like Rx is to provide data structures, like event streams, and procedures, like stream transformations and compositions, that allow dealing with different kinds of events in a uniform way, including representing and processing complex events.&lt;/p&gt;

&lt;p&gt;A simple event stream is bound to a single event source, such as to a DOM event or to the completion of an asynchronuous operation (a &quot;promise&quot;). Thus, when using a reactive programming library, we can define an event stream instead of an event listener. Event streams allow a more uniform handling of different kinds of events. They also allow to handle complex events.&lt;/p&gt;

&lt;p&gt;Derived event streams can be defined on the basis of simple streams representing event sources or other derived streams, with the help of transformations such as event filters and mappings, or with the help of composition operations such as merging two streams.&lt;/p&gt;

&lt;p&gt;For instance, we could define a derived tweet stream by filtering Donald Trump&#039;s tweets using the keywords &quot;Hillary&quot; and merging the resulting stream with a tweet stream by Hillary Clinton filtered with the key word &quot;Donald&quot;. Notice that event stream merging corresponds to a disjunction of event types. Thus, the complex event type &quot;Trump tweet about &#039;Hillary&#039; or Clinton tweet about &#039;Donald&#039;&quot; can be handled by defining a corresponding event stream.&lt;/p&gt;
&lt;/section&gt;

&lt;section&gt;
&lt;h1&gt;Summary&lt;/h1&gt;

&lt;p&gt;Reactive programming libraries like Rx support processing different kinds of simple and complex events in a uniform way with the help of event streams. While they increase developer productivity when dealing with complex events, they are also useful, but not required, for dealing with simple events, due to their elegant syntax.&lt;/p&gt;
&lt;/section&gt;
&lt;/article&gt;
&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;section class=&quot;field field-name-field-category field-type-taxonomy-term-reference field-label-above view-mode-rss&quot;&gt;&lt;h2 class=&quot;field-label&quot;&gt;Category:&amp;nbsp;&lt;/h2&gt;&lt;ul class=&quot;field-items&quot;&gt;&lt;li class=&quot;field-item even&quot;&gt;&lt;a href=&quot;/taxonomy/term/49&quot; typeof=&quot;skos:Concept&quot; property=&quot;rdfs:label skos:prefLabel&quot; datatype=&quot;&quot;&gt;reactive programming&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/section&gt;&lt;div class=&quot;easy_social_box clearfix horizontal easy_social_lang_und&quot;&gt;
            &lt;div class=&quot;easy_social-widget easy_social-widget-twitter first&quot;&gt;&lt;a href=&quot;http://twitter.com/share&quot; class=&quot;twitter-share-button&quot;
data-url=&quot;https://web-engineering.info/node/63&quot;
data-count=&quot;horizontal&quot;
data-lang = &quot;en&quot;
data-via=&quot;&quot;
data-related=&quot;:Check it out!&quot;
data-text=&quot;What Is Reactive Programming and how Does It Work?&quot;&gt;Tweet&lt;/a&gt;&lt;/div&gt;
          &lt;div class=&quot;easy_social-widget easy_social-widget-facebook&quot;&gt;&lt;fb:like href=&quot;https://web-engineering.info/node/63&quot; send=&quot;true&quot; layout=&quot;button_count&quot; width=&quot;88&quot; show_faces=&quot;true&quot; action=&quot;like&quot; colorscheme=&quot;light&quot; font=&quot;&quot;&gt;&lt;/fb:like&gt;&lt;/div&gt;
          &lt;div class=&quot;easy_social-widget easy_social-widget-googleplus&quot;&gt;&lt;div class=&quot;g-plusone&quot; data-size=&quot;medium&quot; data-annotation=&quot;bubble&quot; data-href=&quot;https://web-engineering.info/node/63&quot;&gt;&lt;/div&gt;&lt;/div&gt;
          &lt;div class=&quot;easy_social-widget easy_social-widget-linkedin last&quot;&gt;&lt;script type=&quot;in/share&quot; data-url=&quot;https://web-engineering.info/node/63&quot; data-counter=&quot;right&quot;&gt;&lt;/script&gt;&lt;/div&gt;
  &lt;/div&gt; &lt;!-- /.easy_social_box --&gt;</description>
 <pubDate>Mon, 18 Jul 2016 23:47:05 +0000</pubDate>
 <dc:creator>gwagner</dc:creator>
 <guid isPermaLink="false">63 at https://web-engineering.info</guid>
 <comments>https://web-engineering.info/node/63#comments</comments>
</item>
</channel>
</rss>
