Part 1. Getting started with Reactive Extensions
What are reactive applications? What are they good for? How does programming with Reactive Extensions (Rx) change the way you write code? What should you do before you start working with Rx? And why is Rx better than traditional event-driven programming? These are the questions weâll begin to address in these first three chapters.
Youâll learn what reactive systems and applications are, and why you should care. Youâll see a real example of creating an application that uses Rx and what you need to do to create your own Rx applications. Youâll also look at the functional programming foundations that Rx is based on, to make it easier to understand the rest of the concepts that this book introduces.
Chapter 1. Reactive programming
This chapter covers
- Being reactive
- Thinking about events as streams
- Introducing Reactive Extensions (Rx)
The reactive programming paradigm has gained increasing popularity in recent years as a model that aims to simplify the implementation of event-driven applications and the execution of asynchronous code. Reactive programming concentrates on the propagation of changes and their effectsâsimply put, how to react to changes and create data flows that depend on them.[1]
1
This book is about reactive programming and not about functional reactive programming (FRP). FRP can operate on continuous time, whereas Rx can operate only on discrete points of time. More info can be found at the FRP creatorâs keynote, http://mng.bz/TcB6.
With the rise of applications such as Facebook and Twitter, every change happening on one side of the ocean (for example, a status update) is immediately observed on the other side, and a chain of reactions occurs instantly inside the application. It shouldnât come as a surprise that a simplified model to express this reaction chain is needed. Today, modern applications are highly driven by changes happening in the outside environment (such as in GPS location, battery and power management, and social networking messages) as well as by changes inside the application (such as web call responses, file reading and writing, and timers). To all of those events, the applications are reacting accordinglyâfor instance, by changing the displayed view or modifying stored data.
We see the necessity for a simplified model for reacting to events in many types of applications: robotics, mobile apps, health care, and more. Reacting to events in a classic imperative way leads to cumbersome, hard-to-understand, and error-prone code, because the poor programmer who is responsible for coordinating events and data changes has to work manually with isolated islands of code that can change that same data. These changes might happen in an unpredictable order or even at the same time. Reactive programming provides abstractions to events and to states that change over time so that we can free ourselves from managing the dependencies between those values when we create the chains of execution that run when those events occur.
Reactive Extensions (Rx) is a library that provides the reactive programming model for .NET applications. Rx makes event-handling code simpler and more expressive by using declarative operations (in LINQ style) to create queries over a single sequence of events. Rx also provides methods called combinators (combining operations) that enable you to join sequences of events in order to handle patterns of event occurrences or the correlations between them. At the time of this writing, more than 600 operations (with overloads) are in the Rx library. Each one encapsulates recurring event-processing code that otherwise youâd have to write yourself.
This bookâs purpose is to teach you why you should embrace the reactive programming way of thinking and how to use Rx to build event-driven applications with ease and, most important, fun. The book will teach you step by step about the various layers that Rx is built upon, from the building blocks that allow you to create reactive data and event streams, through the rich query capabilities that Rx provides, and the Rx concurrency model that allows you to control the asynchronicity of your code and the processing of your reactive handlers. But first you need to understand what being reactive means, and the difference between traditional imperative programming and the reactive way of working with events.
1.1. Being reactive
As changes happen in an application, your code needs to react to them; thatâs what being reactive means. Changes come in many forms. The simplest one is a change of a variable value that weâre so accustomed to in our day-to-day programming. The variable holds a value that can be changed at a particular time by a certain operation. For instance, in C# you can write something like this:
int a = 2; int b = 3; int c = a + b; Console.WriteLine("before: the value of c is {0}",c); a=7; b=2; Console.WriteLine("after: the value of c is {0}",c);
The output is
before: the value of c is 5 after: the value of c is 5
In this small program, both printouts show the same value for the c variable. In our imperative programming model, the value of c is 5, and it will stay 5 unless you override it explicitly.
Sometimes you want c to be updated the moment a or b changes. Reactive programming introduces a different type of variable thatâs time varying: this variable isnât fixed to its assigned value, but rather the value varies by reacting to changes that happen over time.
Look again at our little program; when itâs running in a reactive programming model, the output is
before: the value of c is 5 after: the value of c is 9
âMagicallyâ the value of c has changed. This is due to the change that happened to its dependencies. This process works just like a machine thatâs fed from two parallel conveyers and produces an item from the input on either side, as shown in figure 1.1.
Figure 1.1. A reactive representation of the function c = a + b. As the values of a and b are changing, câs value is changing as well. When a is 7 and b is 2, c automatically changes to 9. When b changes to 1, c becomes 8 because aâs value is still 7.
You might find it surprising, but youâve probably worked with reactive applications for years. This concept of reactiveness is what makes your favorite spreadsheet application so easy and fun to use. When you create this type of equation in a spreadsheet cell, each time you change the value in cells that feed into the equation, the result in the final cell changes automatically.
1.1.1. Reactiveness in your application
In a real-world application, you can spot possible time-variant variables in many circumstancesâfor instance, GPS location, temperature, mouse coordinates, or even text-box content. All of these hold a value ...