Introduction to Rx

Dimitris Kalaitzidis
3 min readFeb 22, 2019

--

An API for asynchronous programming with observables streams.

Rx is a combination of the best ideas from the Observer pattern, the iterator pattern, and functional programming.

You can easily create event streams, compose and transform streams with query-like operators, subscribe to any observable stream to perform side effects.

Rx is everywhere and is meant for everything: RxJava, RxJS, rx.NET, RxScala, RxCpp, RxRb, RxPY, RxGO and more… (http://reactivex.io/languages.html)

Personally i started using it on my latest iOS application at ORFIUM (https://www.orfium.com) and I’m asking myself why I didn’t use it before. My personal opinion is that every mobile developer, at least, should give it a try (you will thank me later).

Foundation of Rx

A team at Microsoft took on the challenge of solving the problems of asynchronous, scalable, real-time application development. They worked on a library, independently from the core teams in the company, and sometime around 2009, offered a new client and server side framework called Reactive Extensions for .NET (Rx).

It was an installable add-on for .NET 3.5, and later became a built-in core library in .NET 4.0. It’s been open source component since 2012 and permitted other languages and platforms to reimplement the same functionality, which turned Rx into a cross-platform standard.

Today we have RxJS, RxKotlin, RxSwift, rx.NET, RxScala and more. All these libraries strive to implement the same behaviour and same expressive APIs. Ultimately, a developer creating an iOS app with RxSwift can freely discuss app logic with another programmer using RxJS in the web.

The three building blocks of Rx code are observables, operators and schedulers.

Observables

Observables are the heart of Rx. You may see observable, observable sequence, sequence or stream and really they’re all the same thing. Everything is a sequence.

Observable is just a sequence, with some special powers. One of them, in fact the most important one, is that is asynchronous. Observables produce events, the process of which is referred to as emitting, over a period of time. Events can contain values, such as numbers or instances of a custom type, or they can be recognised gestures, such as taps.

An observable emits next events that contain elements. It can continue to do this until it either:

…emits an error event and is terminated, or

…emits a completed event and is terminated.

Once an observable is terminated, it can no longer emit events.

Lets create an Observable

Creating an Observable in Swift with RxSwift

An observable doesn’t do anything until receives a subscription.

How to subscribe to an Observable?

It’s the subscription that triggers an observable to begin emitting events, up until emits an .error or .completed event and is terminated. We can also manually cause an observable to terminate by cancelling a subscription to it. (dispose)

Subscribe an element handler

Subscribe to an Observable and print the events

The subscription occurs at numbers. subscribe and at that point, the observable is emitting the events.

The output

next(1)
next(2)
next(3)
completed

Subscribe an event handler

Subscribe to an Observable and print the values

The output

1
2
3
completed

I know I can go on and on about observables but I'm trying to keep it simple and make it understandable to someone who happens to explore Rx for the first time.

On my next article, I’ll explore Subjects, an Observable on steroids.

If you enjoyed this article you can follow me on Github (https://github.com/dkalaitzidis/) or Twitter (https://twitter.com/kalaitzidis34).

My next article, RxSwift Subjects: https://medium.com/@dimitriskalaitzidis/rxswift-subjects-a2c9ff32a185

Cheers!

References: reactivex.io, RxSwift: Reactive Programming with Swift (raywenderlich), WikiPedia

--

--