The answer was drafted without focusing too much on the pillars of reactive programming, I left a dedicated section to explain how each of the pillars maps to each of the points of my answer
Definition of reactive
Before explaining what is reactive programming I believe it is worth explaining what is the meaning of reactive in infopedia
Reacting.
That is, something that happens as a result of a previous event/event
Reactive programming
If the concept of reactive programming exists, then there is also the concept of non-reactive programming. I will demonstrate both with the following code:
//Programção não reativa
console.log(prompt('Introduza um texto'));
//programação reactiva
function userInput(msg, action){
action(prompt(msg));
}
userInput('Introduza um texto', console.log);
There is a subtle difference in code snippets. The first can be described as follows:
Opens a dialog for the user to enter text The user
enter text The text you entered is written on the console
While the second can be described as follows
When the user type the text in the window, type the text in the console
With this example you come to the conclusion that whenever you use callbacks you are programming reactively.
You no longer have a line of instructions flow continues in your code. Instead you will use and abuse the callbacks to establish what happens when a given operation is completed.
Reactive programming with events
If you’ve ever worked with user interface then it is almost certain that you have already used reactive programming.
For example, when you add a Handler for a one button event you are to stipulate what happens when the user clicks on the button.
This is a particular example of the use of callbacks previously discussed.
In fact this practice is so well known that there is a paradigm for this form of programming. It is known as Event-oriented programming
Reactive programming and long-term operations
Another place where it is common to see the use of reactive programming is in long-term operations, especially if they have an interface asynchronous. One of the best known among programmers are the AJAX requests.
Let me give you an example similar to one of the questions.
var now = performance.now();
var request = new XMLHttpRequest();
request.onreadystatechange = function A() {
if (request.readyState == 4 && request.status == 200) {
console.log("API SE " + ~~(performance.now() - now) + "ms");
}
}
request.open('GET', 'https://api.stackexchange.com/2.2/answers?order=desc&sort=activity&site=stackoverflow', true);
request.send();
Once again appear the callbacks, you specify what code you want to execute when the request is satisfied by the server and the response reaches your program.
Reactive programming ALL IN 1
If you have come this far you must be thinking that after all you have programmed reactivates and yes virtually all of us have done it.
What’s new now is frameworks that try to provide you with generic Apis to reactively program. More particularly the framework Reactive, available across multiple platforms
This framework joins 3 functionalities in a single API.
- Events
- Dice
- Asynchronous and parallel processing
If I may, I’d like to parallel the language C#
and platform .NET
. Basically the framework joins events with Task
and with the IEnumerable
with the respective methods Linq
and a few extra.
In particular this framework takes special care when dealing with events. Even with a small number of events it is possible to need several ways to process them, namely in relation to the order of events and the possibility of possible failures.
The API already provides you with a rather satisfying set of possibilities but there will be cases where you should take extra care to know if events are actually happening in the order you want, at least that was my experience. You can take a look at framework diagrams, leave here a:
I would like to leave here two examples of using this framework in C#
which have already been useful to me in a professional environment and are relatively simple to understand.
File observer in a directory
public IObservable<FileSystemEventArgs> Watch(string path, bool includeExistingFiles = false)
{
var watcher = new FileSystemWatcher(path, "*.*");
watcher.IncludeSubdirectories = true;
var observable = Observable.FromEventPattern(watcher, "Created")
.Merge(Observable.FromEventPattern(watcher, "Deleted"))
.Merge(Observable.FromEventPattern(watcher, "Changed"))
.Merge(Observable.FromEventPattern(watcher, "Renamed"))
.Select(a => a.EventArgs as FileSystemEventArgs);
if (includeExistingFiles)
{
var currentFiles = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories)
.Select(f => new FileSystemEventArgs(WatcherChangeTypes.Created, Path.GetDirectoryName(f), Path.GetFileName(f)));
observable = observable.Merge(currentFiles.ToObservable());
}
watcher.EnableRaisingEvents = true;
return observable;
}
Running a process and returning an observable
public IObservable<EventArgs> ExecuteProcess(string path, string parameters)
{
var process = new Process() {
StartInfo = new ProcessStartInfo(path, parameters)
{
UseShellExecute = false,
CreateNoWindow = true,
},
EnableRaisingEvents = true
};
var observable = Observable.FromEventPattern(process, "Exited")
.Select(a => (EventArgs)a.EventArgs);
process.Start();
return observable;
}
Reactive completion programming.
I don’t know where the @Yonathan has sought the pillars of reactive programming, but nevertheless seem to me to be fully valid:
- Elastic band: Reacts to demand/load: applications can make use of multiple cores and multiple servers;
- Resilient: Reacts to failures; applications react and recover from software, hardware and connectivity failures;
- Message Driven: Reacts to events (Event driven): instead of composing multiple applications threads synchronous systems
are composed of asynchronous event managers and not
blockers;
- Responsive: Reacts to users: applications that offer rich interactions and "real time" with users.
If you have followed the answer so far you know that all this is true.
IS message driven because you say when do.
IS responsive because only the operation is complete (does not block)
It is resilient. At least Reactive has an extension, which allows you to try to hold event a number of times.
It is elastic. At least in the . net framework decides by Voce how many threads the framework should use.
Answering your other questions
Is a paradigm?
To wikipedia says yes, emphasis mine.
In computing, reactive Programming is an asynchronous Programming
paradigm
I would just like to add that in addition to the event-oriented programming I mentioned earlier, reactive programming brings enough new elements to be considered a different paradigm. Remember the three features I mentioned: Events, Dice, Asynchronous and parallel processing
Only works between local agents or one of them can be remote? It is by
cause of the internet?
It’s not because of internet but because someone determined that there was a problem to solve. As far as I know there was nothing so far that would allow you to combine these three functionalities in an elegant and easy-to-handle way.
Apparently it has to be robust to be classified that way?
Need to run on multiple servers? Or is it an option?
If you paid attention to my answer I did not pay much attention to robustness and much less spoke on several servers.
I just mentioned that it may be possible, with the reactive framework, to try to run certain operation again, if it fails.
Reactive programming may or may not be used in distributed environments, robustness will depend on how your distributed system is implemented as well as all services participating in it.
Excited electrons ( ° ʖ °)
– Denis Rudnei de Souza
This question is very good, and difficult to go into detail due to the complexity it involves the Patterns design, as described at the end of the answer to the other question. An example and the library of Polly that makes the application more Resilient. Many other technologies must be involved to have a Reactive Programming.
– Marco Souza
In a comment of the link you posted, the following link was indicated as the best material ever read by the user: https://github.com/kriskowal/gtor
– perozzo
@bigown I found your question interesting because I also didn’t know what it was exactly, so I went online and found this answer that I found interesting! https://answall.com/questions/55332/o-que-%C3%A9-reactive-Programming-program%C3%A7%C3%A3o-reactive
– mcamara
@Perozzo seems sensational, just so it’s worth the question.
– Maniero
@miltoncamara is in the question.
– Maniero
@Marconciliosouza there is at your discretion, I would like to see the answer.
– Maniero
@bigown now understood that you made a link with the answer you read rs.
– mcamara
@sensational mustache even, had never read about. I started reading now and found it very interesting.
– perozzo
you feel like putting an offering in this question to see if an adventurer appears who understands the subject.
– Marco Souza
@Marconciliosouza there is with you. Yours is good, but it would still be cool some other. Then I want to do others on the subject. I posted in the MVP group and I’m sure there is someone there who would give a great answer, but at the moment I only took one or the other who wanted to complain instead of helping, certainly there are others among the more than 100 with a more positive attitude, who knows one of them answer.
– Maniero
The discussion below is very rich and will add well to the theme. https://stackoverflow.com/questions/1028250/what-is-functional-reactive-programming
– Eduardo Pires
You should have opened a Bounty in the old question. Your question is a duplicate question.
– Bruno Costa
@Brunocosta as it can be duplicated if what I’m asking is everything different, until I show that I read that. Just because the subject is the same is not the same. I can put one in this because so far it has good answers, but that still do not answer 100%.
– Maniero
I see this "reactive programming" as an excellent use of the Observer design principle. I have tired of implementing this at hand on the most diverse platforms of java (desktop, ee, android ...) and I confess that I missed something more standardized. Rxjava fell very well -> https://github.com/ReactiveX/RxJava
– wryel
Have you found your answer ?
– Marco Souza
@Marconciliosouza I think not, I will wait a little, I will try to bring someone who answers
– Maniero