The Observer Pattern is at the core of reactive programming, and observables come in two flavors: hot and cold. This is not explicit when you are coding, so this article explains how to tell the difference and switch to a hot observable. The focus is on hot observables. The concepts here are relevant to all languages that support reactive programming, but the examples are in C#. It's critical to understand the distinction before you start doing reactive programming because it will bring you unstuck if you don't.
Please support this blog by signing up for my course Introduction to Uno Platform.
It's hard to clearly define what Reactive Programming is because it spans so many languages and platforms, and it has overlap with programming constructs like events in C#. I recommend reading through the Wikipedia article because it attempts to give a history of reactive programming and provide objective information.
In a nutshell, reactive programming is about responding to events in the form of sequences (also known as streams) of data. Technically, any programming pattern that deals with this is a form of reactive programming. However, a pattern called the Observer pattern has emerged as the de facto standard for reactive programming. Most programming languages have frameworks for implementing the observer pattern, and the observer pattern has become almost synonymous with reactive programming.
Here are some popular frameworks:
ReactiveX (Java oriented - with implementations for many platforms)
The concept is simple. Observables hold information about observers who subscribe to sequences of notifications. The observable is responsible for sending notifications to all of the subscribed observers.
Note: The publish-subscribe (pub/sub pattern) is a closely related pattern, and although technically different, is sometimes used interchangeably with the observer pattern.
Hot observables start producing notifications independently of subscriptions. Cold observables only produce notifications when there are one or more subscriptions.
Take some time to read up about the observer pattern if you are not familiar. If you start Googling, be prepared for many different interpretations of the meaning. This article explains it well and gives examples in C#. This article is another good article on the topic of hot and cold observables.
A hot observable is simpler because only one process runs to generate the notifications, and this process notifies all the observers. A hot observable can start without any subscribed observers and can continue after the last observer unsubscribes.
On the other hand, a cold observable process generally only starts when a subscription occurs and shuts down when the subscription ends. It can run a process for each subscribed observer. This is for more complex use cases.
Hot Observable Use Case
Let's imagine the simplest use case. The notification publisher is a singleton. It gets instantiated when the app starts and will continue to poll for information throughout the app's lifespan. It will never shut down, and it will send notifications to all instances of the subscriber that subscribe to it.
In C#, observers implement the IObserver<> interface, and observables implement the IObservable<> interface. This is an implementation of the use case in C#. We create the publisher with CreateObservable(), and then two subscribers subscribe. They both receive the notification Hi repeatedly until they unsubscribe, or we can cancel the task. This is a hot observable because the long-running task runs independently of the subscribers.
Note: this is not the recommended approach. This is just an example for clarity.
View this gist on GitHub
The reactive extensions are a set of C# helpers for building observables and observers. They exist in the namespace System.Reactive. Their home is in this is repo. You can use it by installing the System.Reactive NuGet package. It would help if you used these extensions instead of directly implementing IObservable<> or IObserver<>. Reactive frameworks for other platforms have similar libraries.
Observable.Create from the reactive extensions creates observables. What the documentation doesn't tell you is that the observable is cold by default. The code that you supply doesn't run unti...
The ideal candidate will have released multiple Xamarin apps commercially and will have a proven track record working as part of a cross functional scrum team. We are seeking proactive, technology focused individuals with best in class development principles. On this team, coding is only part of what we do. Our real focus is on delivery, with backend, frontend and QA working closely together to create, deploy and test solutions. Our solutions will be deployed to a variety of complex environments and used by thousands of hospital patients and staff worldwide. Expect a rewarding and challenging experience.
MyCredit is a space with a huge soul and a pleasant atmosphere. We are a product company, a leader in the microcredit market with 4 years of experience! We are included in the Top 3 FinTech and e-commerce projects of the country!
We believe in the potential of each person, in his talent and desire to develop, and we also believe in respect and support. Therefore, we have assembled a team of guys who share our faith and go forward with us. If all this is about you, be sure to read it to the end!
Our team of professionals is rapidly gaining momentum, and now we are looking for a Middle .NET Developer who will be interested in working with BPM Corezoid.
MyCredit is a space with a huge soul and a pleasant atmosphere. We are a product company, a leader in the microcredit market with 4 years of experience! We are entering the Top 3 FinTech and e-commerce projects of the country!
We believe in the potential of each person, in his desire to develop, and we also believe in respect and support. Therefore, we have assembled a team of guys who share our faith and go forward with us. If all this is about you, be sure to read it to the end!
Our team of professionals is rapidly gaining momentum, and now we are looking for a Lead .NET Developer to work on a high-load project.