Reactive programming

21
Reactive Programming Nick Hodge Developer, Geek [email protected] http://nickhodge.com / https:// gist.github.com/nickhodge for codesnippets

description

Training given to FirebootCamp attendees 15th August 2014 on Reactive Programming.

Transcript of Reactive programming

Page 1: Reactive programming

Reactive ProgrammingNick Hodge

Developer, [email protected]

http://nickhodge.com/ https://gist.github.com/nickhodge for codesnippets

Page 2: Reactive programming

Who is Nick Hodge?

• 28 years in IT, mostly as non-Developer• Apple, Adobe, Microsoft• Many small-scale, rapidly built applications• Open source contributor• Functional Reactive Programming Fan

Page 3: Reactive programming

Group Experiment: Data-at-rest vs. Data-in-motion• LINQ to IEnumerable (or IQueryable)• “data at rest”• yield• What happens if data changes in midst of foreach() ?

• Reactive programming (IObservable)• “data in motion”• Data (or events) are pushed at you

Page 4: Reactive programming

Reactive Programmingdemo required

Page 5: Reactive programming

Reactive Programming vs. TPL

• Task Parallel Library (TPL) http://msdn.microsoft.com/en-us/concurrency • PLINQ

Page 6: Reactive programming

The core of LINQis the sequence

Page 7: Reactive programming

A sequence is just some stuff in a particular order

Page 8: Reactive programming

What about Rx?Let’s talk about events

Page 9: Reactive programming

Limitations of .NET Events

exchange.StockTick += (sender, args) =>{ if (args.Quote.Symbol == “MSFT”) { // Imperative code }};

exchange.StockTick -= /* what goes here? */;

Can’t pass around Hidden data source

Hard resource maintenance

Lack of composition

Page 10: Reactive programming

Observable Sequences to the Rescue

IObservable<Quote> stockQuotes = …;

var msft = stockQuotes

.Where(quote => quote.Symbol == “MSFT”);

var subscription = msft.Subscribe(quote => /* … */);

subscription.Dispose();

Objects can be passed Source of Quotes

Easy resource maintenance

Can define query operators

Page 11: Reactive programming

Are .NET Events Obsolete?• .NET Events

• Code centric

• Design-time experience

• Not first class

• Non-compositional

• Lightweight

• Rigid execution model (IL)

• Observables

• Data centric

• No design-time experience

• First class objects

• Rich composition

• Slightly more cost

• Translatable with expression trees

Page 12: Reactive programming

The Event Processing Landscape

Socialmedia

Stock tickers

RSS feeds

GPS

Server managementUI e

vents

Page 13: Reactive programming

Reactive Extensions Architecture

Concurrency

IScheduler TimeThreads Cloud Dispatchers

Event Streams

IObservable<T>

IObserver<T>ISubject<T>

LINQ to Events

from quote in stockwhere quote.Symbol == “MSFT”select quote.ValueProjectionFi

lterin

g

AggregatingG

roup

ing

Joins

Windowing

Sharing

Sampling

Throttling Timeout

Merging

Recovery

Page 14: Reactive programming

Event Streams

• Towards a unified programming model• Producers are observable sequences

• .NET events, WinRT events, sensor APIs, APM methods, tasks, etc.• Consumers are observers

• Hooking up “continuations” or handlers

Observable

Subscribe

Observer

Page 15: Reactive programming

Essential Interfacesnamespace System

{

public interface IObservable<out T>

{

IDisposable Subscribe(IObserver<T> observer);

}

public interface IObserver<in T>

{

void OnNext(T value);

void OnError(Exception error);

void OnCompleted();

}

}

Page 16: Reactive programming

Observable Sources

• Single items / empty Observable.Return()• Lists and arrays• UI Events• Async methods• Subjects (demo)• Property Changes

Page 17: Reactive programming

Cold / Hot

• Hot : example MouseMove• Cold: when you subscribe, something happens (it waits)

Page 18: Reactive programming

UI Example, Windows 8 Universal App

Page 19: Reactive programming

This Presentation’s Reference Presentations• Bart De Smet “Curing Your Event Processing Blues with Reactive

Extensions (Rx)” TechEd Europe 2012 http://channel9.msdn.com/events/TechEd/Europe/2012/DEV413

• Paul Betts “Introduction to Reactive Extensions” Norwegian Developer’s Conference 2012 http://vimeo.com/43659034

• Brendan Forster “Reactive UI – Turning MVVM up to 11” http://vimeo.com/97329155

Page 20: Reactive programming

Further References, Links

• https://rx.codeplex.com/ and https://github.com/Reactive-Extensions/Rx.NET as the starting point• RxJS (and other languages such as Ruby, C++, Python, Java, ObjC)

https://github.com/Reactive-Extensions/RxJS • http://www.introtorx.com/ • 101 Rx Examples

http://rxwiki.wikidot.com/101samples • http://amzn.to/programming-rx “Programming Reactive Extensions

and LINQ”