SlideShare a Scribd company logo
Code Reactions – An
Introduction to
Reactive Extensions
Jason Bock
Practice Lead
»http://www.magenic.com
»http://www.jasonbock.net
»https://www.twitter.com/jasonbock
»https://www.github.com/jasonbock
»jasonb@magenic.com
Personal Info
https://github.com/JasonBock/
CodeReactions
Downloads
»Why Rx?
»Using Rx
Overview
Remember…
https://github.com/JasonBock/
CodeReactions
Why Rx?
http://frmilovan.files.wordpress.com/2010/05/ford-assembly-line.jpg
Why Rx?
http://icons.iconarchive.com/icons/3xhumed/tools-hardware-pack-4/256/CPU-Z-icon.png
Why Rx?
MainWindow.xaml
<Button
Click="OnStartListeningClick" />
MainWindow.xaml.cs
private void OnStartListeningClick(
object sender, RoutedEventArgs e)
{
// ...
}
Why Rx?
http://www.lolroflmao.com/wp-content/uploads/2012/05/wrong-wrong-wrong.jpg
Why Rx?
http://www.redorbit.com/media/uploads/2013/03/ChemicalReactions_031513-617x416.jpg
http://icyroadsafety.com/parts/blackice1.jpg
http://www.venelogia.com/uploads/mandrake/2012-movie.jpg
Why Rx?
http://icons.iconarchive.com/icons/3xhumed/tools-hardware-pack-4/256/CPU-Z-icon.png
Why Rx?
http://techblog.netflix.com/2013/01/reactive-programming-at-netflix.html
http://savas.me/2014/04/reactive-computing-at-the-heart-of-cortana/
http://reactconf.com/
Why Rx?
Why Rx?
http://2.bp.blogspot.com/-6m8BQlS7-
g8/UArVdKi8yJI/AAAAAAAADFs/tcUDGbkm9Hg/s1600/ChemicalExplosion.jpg
Why Rx?
http://wondrouspics.com/wp-content/uploads/2012/04/bomb-explosion-nuclear-explosions-wallpaper-
540x337.jpg
Why Rx?
StocksDB
What is the current stock
price for MSFT?
37.96 (3/3/2014, 8:32:00 AM Central)
Why Rx?
StocksDB
What is the current stock
price for MSFT?
Why Rx?
StocksDB
37.97
37.98
Tell me when the stock
price changes for MSFT
Why Rx?
Comment feeds
Sensor data
Key logging
Navigation events
File watching
UI events
Game play
Why Rx?
http://www.reactivemanifesto.org/images/full-reactive.png
Why Rx?
http://rehansaeed.co.uk/reactive-extensions-part1-replacing-events/
Using Rx
.NET
Java
C++
Ruby
JavaScript
Python
https://github.com/Reactive-Extensions
Using Rx
Using Rx
http://blog.sudobits.com/wp-content/uploads/2011/08/keylogging-on-ubuntu.jpg
» In .NET 4.0
› IObserver<T>
− OnCompleted()
− OnError(Exception e)
− OnNext(T value)
› IObservable<T>
− Subscribe(IObservable<T> observer)
Using Rx
Demo: Observe Key Presses
Code Reactions – An Introduction to Reactive Extensions
Using Rx
public class MyObservable
: IObservable<char>
Observable.FromEventPattern(...)
Demo: Creating Observables
Code Reactions – An Introduction to Reactive Extensions
Using Rx
the quick brown
fox jumped over
the lazy dog
ECBFEDEEADG.Where(…) .Take(…) 6.4 seconds
Demo: Modifying Observables
Code Reactions – An Introduction to Reactive Extensions
Using Rx
the quick brown
fox jumped over
the lazy dog
OnError()
Retry()
Demo: Exceptions and Observables
Code Reactions – An Introduction to Reactive Extensions
Using Rx
http://www.teetshirts.com/images/uploads/volume-to-11.jpg
Using Rx
RESTAPI Keylogger
Web API
SignalR
Demo: Keylogger Pipeline
Code Reactions – An Introduction to Reactive Extensions
Using Rx
http://social.eli.ubc.ca/files/2011/08/fail.jpg
Using Rx
http://nimitdholakia.files.wordpress.com/2011/01/time.jpg
Demo: Unit Testing and Rx
Code Reactions – An Introduction to Reactive Extensions
Using Rx
http://www.dotnetfoundation.org/
Code Reactions – An
Introduction to
Reactive Extensions
Jason Bock
Practice Lead
Remember…
 https://github.com/JasonBock/CodeReactions
 http://www.slideshare.net/jasonbock/code-reactions-an-
introduction-to-reactive-extensions
 References in the notes on this slide

More Related Content

PPTX
Metaprogramming in .NET
PPTX
Clean Code - A&BP CC
PPTX
Improving code quality with Roslyn analyzers
PDF
Rooted 2010 ppp
ODP
Introduccion a Jasmin
PDF
Testing JavaScript Applications
PPTX
Hypermedia-driven Web Services with Spring Data REST
PDF
Clojure: Simple By Design
Metaprogramming in .NET
Clean Code - A&BP CC
Improving code quality with Roslyn analyzers
Rooted 2010 ppp
Introduccion a Jasmin
Testing JavaScript Applications
Hypermedia-driven Web Services with Spring Data REST
Clojure: Simple By Design

What's hot (14)

PDF
Growing pains - PosKeyErrors and other malaises
PDF
Cooking with Chef
PDF
PHPUnit Episode iv.iii: Return of the tests
PDF
Test Driven Documentation with Spring Rest Docs
PDF
HTML5, the open web, and what it means for you -Tech4Africa
PDF
Getting Testy With Perl6
PPTX
Preparing a WordPress Plugin for Translation
PDF
Como NÃO testar o seu projeto de Software. DevDay 2014
PDF
A Modest Introduction To Swift
PDF
Narrow Down What to Test
PDF
Effective Benchmarks
PPTX
Javascript - The Stack and Beyond
PDF
Speeding up Red Team engagements with carnivorall
PDF
2013-06-25 - HTML5 & JavaScript Security
Growing pains - PosKeyErrors and other malaises
Cooking with Chef
PHPUnit Episode iv.iii: Return of the tests
Test Driven Documentation with Spring Rest Docs
HTML5, the open web, and what it means for you -Tech4Africa
Getting Testy With Perl6
Preparing a WordPress Plugin for Translation
Como NÃO testar o seu projeto de Software. DevDay 2014
A Modest Introduction To Swift
Narrow Down What to Test
Effective Benchmarks
Javascript - The Stack and Beyond
Speeding up Red Team engagements with carnivorall
2013-06-25 - HTML5 & JavaScript Security
Ad

Recently uploaded (20)

PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Digital Strategies for Manufacturing Companies
PPTX
history of c programming in notes for students .pptx
PPT
Introduction Database Management System for Course Database
PPTX
assetexplorer- product-overview - presentation
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
Computer Software and OS of computer science of grade 11.pptx
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
System and Network Administraation Chapter 3
PDF
Understanding Forklifts - TECH EHS Solution
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Upgrade and Innovation Strategies for SAP ERP Customers
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Digital Strategies for Manufacturing Companies
history of c programming in notes for students .pptx
Introduction Database Management System for Course Database
assetexplorer- product-overview - presentation
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Computer Software and OS of computer science of grade 11.pptx
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
CHAPTER 2 - PM Management and IT Context
Operating system designcfffgfgggggggvggggggggg
PTS Company Brochure 2025 (1).pdf.......
System and Network Administraation Chapter 3
Understanding Forklifts - TECH EHS Solution
wealthsignaloriginal-com-DS-text-... (1).pdf
Ad

Code Reactions - An Introduction to Reactive Extensions

Editor's Notes

  • #6: For many years, programs were written in a synchronous fashion. It felt like the flow of an assembly line. You do action 1, then action 2, then action 3, etc.
  • #7: This was primarily due to the fact that we only had one core in our machines.
  • #8: Specifically, event handling has been a synchronous affair. You want to listen to a button click? Just wire up a button click, and do everything synchronously and have an unresponsive UI.
  • #9: That’s wrong! We live in an asynchronous, reactive world. It’s time to start viewing the programming world as it really is.
  • #10: The world is full of reactions.
  • #11: And with multiple cores in our machines, we can handle these actions in a way that provides a responsive application to the user
  • #12: The idea of reactive programming is taking hold in a number of applications and in the software development community as a whole: Netflix’s site, both in the client (JavaScript) and server (Java and JavaScript) are completely reactive (http://techblog.netflix.com/2013/01/reactive-programming-at-netflix.html) Cortana is powered via Bing’s streaming data services, which uses reactive programming concepts (http://savas.me/2014/04/reactive-computing-at-the-heart-of-cortana/) There was a conference dedicated completely to the idea of reactive programming in London (http://reactconf.com/)
  • #13: In fact, reactive programming has been around for quite some time. Excel has been used and over-abused to do all sorts of things in software development. But at its core, it’s always done reactive programming. That is, if I change the value in either A1 or B1, C1 is updated automatically. So, just like everything in software development, “this is nothing new” .
  • #14: We want to handle those reactions and events carefully, lest things blow up in our faces.
  • #15: Or things get really out of hand!
  • #16: But let’s do a more complex example. Let’s say we wanted to get the price of a stock. So, we ask some kind of data source what the price of a stock is, and we get an answer. Note, this is a very simplistic view of the stock market. Trading happens MUCH faster than this, see the “Ghost Exchange” documentary for details on algorithmic trading, where hundredths of a second matter.
  • #17: But stock prices can change very rapidly. We could try to keep pinging the data source, but that would tie up precious resources in the DB, in the network, and in the UI
  • #18: Instead, let’s react to the changes in price.
  • #19: What kind of data sources are reactionary? A lot
  • #20: Reactive programming embraces asynchronous data streams. It’s based on 4 principles: Responsive – being able to react quickly to changes in the application (e.g. multiple users editing the same document. The less latency, the increase in user interaction Scalable – Having the ability to scale quickly. Using messages and loose coupling with asynchronous programming models provides that foundation Resilient – A failure in one part of the system doesn’t adversely affect the system as a whole Event-driven – Using a push model is key (+ asynchronous, non-blocking)
  • #21: Reactive Extensions intends to make interactions with asynchronous producers simple for .NET developers.
  • #22: While our talk is going to be focused on .NET (C#), there are other implementations of the idea of reactive programming in other languages.
  • #23: There are a couple of ways to get the Rx assemblies – the easiest is to use NuGet
  • #24: For our example, we’ll be evil and use a keylogger . This allows us to generate a lot of data to react to.
  • #25: To start using Rx in .NET, realize that two core interfaces are part of .NET since 4.0
  • #26: This demo will show how you can implement IObserver<T> and IObservable<T>
  • #27: So that shows the basics, but frankly, implementing your own observer or observable isn’t recommended. Nor is using something called a subject. While it’s a good introduction to Rx, we need to switch gears and dive into easy ways to create and use observables in Rx.
  • #28: Now let’s show how you can create an observer and an observable without implementing them directly
  • #29: Let’s do more advanced things with the observable. Maybe we want to filter data so we only see a set of values when we subscribe. And we also want to know how long it takes to produce 10 of those values.
  • #30: Let’s show how we can filter and time observables
  • #31: What happens if we have errors? There are lots of ways to handle exceptions and provide control flow in this case. Let’s say in this case a “q” character always messes things up for some reason. You can provide a handler for OnError(), but that just stops the subscription process cold. You can also use Retry() to “skip over” the error and keep going through the sequence. (There’s also a VB-like OnErrorResumeNext()).
  • #32: Let’s show how we can filter and time observables
  • #33: Now that we’ve seen some basics, let’s crank it to 11. We want to get away somewhat from the keylogger being the main focus, and have it just be a pure producer via storing its captured data to a web site. We’ll do some fun things now in a client to react to those captures.
  • #34: Here’s the change. The keylogger will cache a set of key captures, and send those values over to a web API. That, in turn, will broadcast new key captures to anyone who is listening via SignalR. We’ll set up a Windows app that will be listening to those broadcast, append the character data to a text box…and produce some tones.
  • #35: Let’s show how we put it all together
  • #36: One thing to keep in mind is testing. Demo code is OK, but in the “real world” you’ll write tests with code that observe events, maybe use IObservables directly, etc.
  • #37: We also have to be cognizant about time. If the subscription has a throttling aspect to it, we probably don’t want to wait for that throttling time. Thankfully, Rx has ways to address that in tests.
  • #38: Let’s go over a couple of unit testing scenarios
  • #39: One thing to keep in mind, Rx is now part of the .NET Foundation. Check out all the cool OSS projects within this foundation (including Roslyn!)
  • #40: References Rx on GitHub - https://github.com/Reactive-Extensions Rx Wiki - http://rxwiki.wikidot.com/ ReactiveX - http://reactivex.io/ “Async, await, and yield return” - http://www.interact-sw.co.uk/iangblog/2013/11/29/async-yield-return "The Reactive Manifesto" - http://www.reactivemanifesto.org/ "Introduction to Rx" - http://www.introtorx.com/ "Master time with Reactive Extensions" - http://haacked.com/archive/2014/03/10/master-time-with-reactive-extensions/ "Async JavaScript with Reactive Extensions" - https://www.youtube.com/watch?v=XRYN2xt11Ek “Gesture recognition with Leap Motion controller and Reactive Extensions” - http://blog.zuehlke.com/en/gesture-recognition-with-leap-motion-controller-and-reactive-extensions/ “Notification Patterns in Rx” Part 1: Introduction - http://geekswithblogs.net/TimothyK/archive/2014/08/29/notification-patterns-in-rx-part-i-introduction.aspx Part 2: Creating a Stream - http://geekswithblogs.net/TimothyK/archive/2014/08/29/notification-patterns-in-rx-part-ii-creating-a-stream.aspx Part 3: Lossless Notification - http://geekswithblogs.net/TimothyK/archive/2014/08/29/notification-patterns-in-rx-part-iii-lossless-notifications.aspx Part 4: Unexpected Errors - http://geekswithblogs.net/TimothyK/archive/2014/08/29/notification-patterns-in-rx-part-iv-unexpected-errors.aspx Part 5: Connection Errors - http://geekswithblogs.net/TimothyK/archive/2014/08/29/notification-patterns-in-rx-part-v-connection-errors.aspx SignalR References "Tutorial: Getting Started with SignalR 2.0 and MVC 5" - http://www.asp.net/signalr/overview/signalr-20/getting-started-with-signalr-20/tutorial-getting-started-with-signalr-20-and-mvc-5 "ASP.NET SignalR Hubs API Guide - .NET Client" - http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-net-client "Better Together – SignalR and the Rx Framework" - http://blog.safaribooksonline.com/2014/02/10/signalr-rx-framework/ "SignalR and Reactive Extensions are an Rx for server push notifications" - http://www.thinqlinq.com/Post.aspx/Title/SignalR-and-Reactive-Extensions-are-an-Rx-for-server-push-notifications NAudio References NAudio on Codeplex - http://naudio.codeplex.com/ "Fire and Forget Audio Playback with NAudio" - http://mark-dot-net.blogspot.co.uk/2014/02/fire-and-forget-audio-playback-with.html "Playback of Sine Wave in NAudio" - http://mark-dot-net.blogspot.com/2009/10/playback-of-sine-wave-in-naudio.html "Sound Generation in WPF Applications" - http://msdn.microsoft.com/en-us/magazine/ee309883.aspx