concept IObservable in category c#

This is an excerpt from Manning's book Functional Programming in C#: How to write better C# code.
With an IObservable, you can observe the values as they come. Table 14.1 shows how IObservable relates to other abstractions.
Table 14.1. How IObservable compares with other abstractions
Synchronous
Asynchronous
Single value T Task<T> Multiple values IEnumerable<T> IObservable<T> IObservable is like an IEnumerable, in that it contains several values, and it’s like a Task, in that values are delivered asynchronously. IObservable is therefore more general than both: you can view IEnumerable as a special case of IObservable that produces all its values synchronously; you can think of Task as a special case of IObservable that produces a single value.
The easiest way to develop an intuition about IObservable is through marble diagrams, a few examples of which are shown in figure 14.1. Marble diagrams represent the values in the stream. Each IObservable is represented with an arrow, representing time, and marbles, representing values that are produced by the IObservable.
The image illustrates that an IObservable can actually produce three different kinds of messages:
OnNext signals a new value, so if your IObservable represents a stream of events, OnNext will be fired when an event is ready to be consumed. This is an IObservable’s most important message, and often the only one you’ll be interested in. OnCompleted signals that the IObservable is done and will signal no more values.
The IObservable contract specifies that an IObservable should produce messages according to the following grammar:
OnNext* (OnCompleted|OnError)?That is, an IObservable can produce an arbitrary number of T’s (OnNext), possibly followed by a single value indicating either successful completion (OnCompleted) or an error (OnError).
This means that there are three possibilities in terms of completion. An IObservable can
As you’ve seen, having an abstraction that captures a data stream enables you to detect patterns and specify logic that spans across multiple events, within the same stream or across different streams. This is where I’d recommend using IObservable. The corollary is that, if your events can be handled independently, then you probably shouldn’t use IObservables, because using them will probably reduce the readability of your code.
A very important thing to keep in mind is that because OnNext has no return value, an IObservable can only push data downstream, and never receives any data back. Hence, IObservables are best combined into one-directional data flows. For instance, if you read events from a queue and write some data into a DB as a result, IObservable can be a good fit. Likewise if you have a server that communicates with web clients via WebSockets, where messages are exchanged between client and server in a fire-and-forget fashion. On the other hand, IObservables are not well-suited to a request-response model such as HTTP. You could model the received requests as a stream and compute a stream of responses, but you’d then have no easy way to tie these responses back to the original requests.