Rx – for beginners (part 9): Hot Vs. Cold observable


Rx – for beginners (part 9): Hot Vs. Cold observable

Rx, IObservable,IObserver this post is the 9th in a series of posts about the new Reactive Framework (Rx).

the series TOC can found here.

in this post we will focus on the Hot Vs. Cold observable stream.


the code for this post can be download from here.


If a tree falls in a forest and no one is around to hear it,
does it make a sound?

if it do make a sound when nobody observed it, we should mark it as hot,

otherwise it should be marked as cold.


What is Hot observable?

hot observable streams are streams that is active regardless whether or not

they are being observed.

such stream are the mouse move event or timer event.


What is Cold observable?

cold observable streams are streams that is activated upon subscription

(in most case new stream will be activate for each subscription).

the Observable.Interval is a sample for cold observable stream.


Code Sample

the code for this post can be download from here.


the following sample demonstrate the difference between hot and cold observable streams:

Code Snippet
  1. var timer = new System.Timers.Timer(1000);
  2. // create Cold observable stream (this stream will not project values
  3. //      until the subscription, and will project new stream for each subscription)
  4. var coldStream = Observable.Interval(TimeSpan.FromSeconds(1));
  6. // create Hot observable stream out of .NET event
  7. var hotStream = Observable.FromEvent<ElapsedEventArgs>(timer,"Elapsed");
  8. DateTime startTime = DateTime.Now;
  9. timer.Start(); // start the timer (the timer will start producing values
  10.                  // regardless of the subscription)
  11. // convert the hot stream to stream that project the amount of seconds from the timer start
  12. var hotStreamInterval = hotStream.Select ( value => (value.EventArgs.SignalTime – startTime).TotalSeconds.ToString("N0"));
  14. Thread.Sleep(TimeSpan.FromSeconds(3)); // sleep for 3 seconds befor the subscription
  15. using (hotStreamInterval.Subscribe (value => Write("Hot: " + value, ConsoleColor.Green)))
  16. using(coldStream.Subscribe(value => Write("\t\tCold 1: " + value,ConsoleColor.Yellow)))
  17. {
  18.     Thread.Sleep(TimeSpan.FromSeconds(2)); // sleep for 2 seconds befor the second subscription
  19.     // second subscription
  20.     using(coldStream.Subscribe(value => Write("\t\t\t\tCold 2: " + value,ConsoleColor.DarkYellow)))
  21.     {
  22.         Thread.Sleep(TimeSpan.FromSeconds(4)); // wait 4 second before disposal
  23.     } // dispose hotStreamInterval and coldStream 2
  24. } // dispose hotStreamInterval and coldStream 1

line 1, create system timer with 1 second interval.

line 4, define cold observable stream with interval of 1 second.

line 7, creating hot observable stream out of the timer elapsed event (we can use this technique on any .NET event).

line 9, start the timer (because this is hot stream the timer will not wait for the subscription).

line 12, format the timer stream into more friendly observable stream that will project the timer duration in seconds.

line 14, sleeping 3 second before the subscription (this will make the difference between the hot streams that will

            increase the value even though no body is listening and the cold stream that doesn’t construct until the subscription).

line 15, subscribing to the hot observable stream (the using statement is the subscription scope).

line 16, first subscription to the cold observable stream.

line 18, sleeping before the second subscription to the cold stream.

line 20, second subscription to the cold observable stream (the second subscription will construct new observable stream).

line 22, waiting 4 second before leaving the subscription scope.


the output will look as follow:

IObservable, Rx, IObserver

  • the hot timer observable stream (green) is starting with value = 3 because it is  hot stream
    which was producing value during the 3 second sleeping period
  • the first cold observable stream (yellow) is starting with value = 0 because the stream were constructed after
    the sleep period.
  • the second cold observable stream (dark yellow) is starting with value = 0 because each subscription
    is constructing new stream.



 Rx, IObservable,IObserver

subscribing to hot observable stream will produce different result

from cold observable stream subscription, and it is important to

be aware for the nature of the stream you subscribing to.




תגים של Technorati:‏ ,,


kick it on DotNetKicks.com

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>