reactive programming & i t: perfect marriagereactiveprogramming_iot_svenbeaupr… · • unix...

36
REACTIVE PROGRAMMING & IOT: A PERFECT MARRIAGE Sven Beauprez, IoTBE.org

Upload: others

Post on 14-Oct-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: REACTIVE PROGRAMMING & I T: PERFECT MARRIAGEreactiveprogramming_iot_svenbeaupr… · • Unix pipes are an example of linear flow based programming while a stream of data follows

REACTIVE PROGRAMMING & IOT: A PERFECT MARRIAGE Sven Beauprez, IoTBE.org

Page 2: REACTIVE PROGRAMMING & I T: PERFECT MARRIAGEreactiveprogramming_iot_svenbeaupr… · • Unix pipes are an example of linear flow based programming while a stream of data follows

2

•  Steering member of IoT BE -  http://www.iotbe.org -  http://www.meetup.com/iot_be/

•  Steering member of JavaPolis/Devoxx (2002 – 2009)

•  Enterprise Solution Architect & Integration Architect •  Entrepreneur & Evangelist of cutting edge internet technologies

-  In the middle of founding several startups around IoT

•  Keep in touch -  [email protected] -  https://www.linkedin.com/in/svenbeauprez -  @SvenBeauprez and @iot_bE

WHO AM I?

Page 3: REACTIVE PROGRAMMING & I T: PERFECT MARRIAGEreactiveprogramming_iot_svenbeaupr… · • Unix pipes are an example of linear flow based programming while a stream of data follows

3

•  One of the larger IoT communities in Europe (>850 members) -  http://www.iotbe.org -  http://www.meetup.com/iot_be/

•  Regular meetups around IoT and providing guest speakers for international conferences

•  We are looking for international partners for EU Funding (Horizon 2020 Funding)!

•  We are always looking for experienced (international) speakers for our meetups!

IOT BE

Page 4: REACTIVE PROGRAMMING & I T: PERFECT MARRIAGEreactiveprogramming_iot_svenbeaupr… · • Unix pipes are an example of linear flow based programming while a stream of data follows

4

IOT WORLD FORUM REFERENCE MODEL

1. Physical devices (the “Edge”)

2. Connectivity

3. Edge computing (the “Fog”)

4. Data Accumulation 5. Data Abstraction

(in the “Cloud”)

6. Application

7. Collaboration & Processes

Actuators and sensors send and receive data via

sending or receiving it via the internet to/from

(big) data stores from which data can be retrieved or stored

so people can act upon it

by devices where it is processed and visualised

via the internet

gateways which filters and aggregates it before

a network towards

Page 5: REACTIVE PROGRAMMING & I T: PERFECT MARRIAGEreactiveprogramming_iot_svenbeaupr… · • Unix pipes are an example of linear flow based programming while a stream of data follows

5

INTERNET OF THINGS VS. INTERNET OF EVERYTHING

Page 6: REACTIVE PROGRAMMING & I T: PERFECT MARRIAGEreactiveprogramming_iot_svenbeaupr… · • Unix pipes are an example of linear flow based programming while a stream of data follows

6

INTERNET OF THINGS VS. INTERNET OF EVERYTHING

Page 7: REACTIVE PROGRAMMING & I T: PERFECT MARRIAGEreactiveprogramming_iot_svenbeaupr… · • Unix pipes are an example of linear flow based programming while a stream of data follows

7

•  … runs as a single process -  Does not start a new process for each new request, which would mean a lot of overhead

•  … runs applications in a single thread -  Does not start a new thread for each new request, which means less complex code and

avoiding race conditions

•  … is asynchronous -  Can handle multiple requests at one time without blocking new requests by using I/O

eventing, callbacks and an event loop

•  … is ideal for I/O intensive tasks, but less for CPU intensive -  CPU intensive tasks within the application would block the process and must be offloaded in

worker threads/processes

EVENT PROCESSING WITH NODE.JS

In other words a perfect fit for IoT where the main task on the edges and in the fog is collecting sensor data

Page 8: REACTIVE PROGRAMMING & I T: PERFECT MARRIAGEreactiveprogramming_iot_svenbeaupr… · • Unix pipes are an example of linear flow based programming while a stream of data follows

8

NODE.JS EVENT LOOP

waiting waiting

Incoming request

Query DB (offloaded)

Process Result

Write log (offloaded)

Respond

Event Queue

Event Loop (Single Thread)

Thread Pool & Async IO

Filesystem

Network

Other

*http://kunkle.org/nodejs-explained-pres

Page 9: REACTIVE PROGRAMMING & I T: PERFECT MARRIAGEreactiveprogramming_iot_svenbeaupr… · • Unix pipes are an example of linear flow based programming while a stream of data follows

9

•  Naive example of a Node.js appication

NODE.JS CALLBACK HELL

callback 1

callback 2

callback 3

Confusing, this gives even wrong results!

Page 10: REACTIVE PROGRAMMING & I T: PERFECT MARRIAGEreactiveprogramming_iot_svenbeaupr… · • Unix pipes are an example of linear flow based programming while a stream of data follows

10

•  Also called futures, deffereds, eventuals,… •  A promise is an object (proxy) that represents the result of an asynchronous

function call

•  Biggest advantage of promises is that they can be chained to avoid callback hell

•  Promises can return any other value and the next 'onFulfilled' will be passed the value as argument

NODE.JS AND PROMISES

Promises are a simple form of Dataflow programming

Page 11: REACTIVE PROGRAMMING & I T: PERFECT MARRIAGEreactiveprogramming_iot_svenbeaupr… · • Unix pipes are an example of linear flow based programming while a stream of data follows

11

•  Traditionally a program is modeled as a series of operations happening in a specific order, in other words how to do computations

WHAT IS DATAFLOW?

Source: http://www.autobild.de/

Page 12: REACTIVE PROGRAMMING & I T: PERFECT MARRIAGEreactiveprogramming_iot_svenbeaupr… · • Unix pipes are an example of linear flow based programming while a stream of data follows

12

•  Dataflow programming emphasizes the movement of data and models programs as a series of connections. It describes what computation should be performed

WHAT IS DATAFLOW?

Source: http://commons.wikimedia.org/wiki/File:Hyundai_car_assembly_line.jpg

Page 13: REACTIVE PROGRAMMING & I T: PERFECT MARRIAGEreactiveprogramming_iot_svenbeaupr… · • Unix pipes are an example of linear flow based programming while a stream of data follows

13

•  Describes a graph of nodes, which exchange messages containing data via the edges.

•  The edges are defined outside the nodes, in others words nodes have no control on where the data comes from and where it goes to

FLOW BASED PROGRAMMING (FBP)

Page 14: REACTIVE PROGRAMMING & I T: PERFECT MARRIAGEreactiveprogramming_iot_svenbeaupr… · • Unix pipes are an example of linear flow based programming while a stream of data follows

14

•  Unix pipes are an example of linear flow based programming while a stream of data follows the pipes and the different 'smaller' dedictated applications run in parallel

FBP: UNIX PIPES

Page 15: REACTIVE PROGRAMMING & I T: PERFECT MARRIAGEreactiveprogramming_iot_svenbeaupr… · • Unix pipes are an example of linear flow based programming while a stream of data follows

15

FBP: YAHOO PIPES

Source: https://pipes.yahoo.com/pipes/pipe.info?_id=ZKJobpaj3BGZOew9G8evXg

Page 16: REACTIVE PROGRAMMING & I T: PERFECT MARRIAGEreactiveprogramming_iot_svenbeaupr… · • Unix pipes are an example of linear flow based programming while a stream of data follows

16

FBP: ENTERPRISE APPLICATION INTEGRATION

http://www.eaipatterns.com/Chapter1.html

Page 17: REACTIVE PROGRAMMING & I T: PERFECT MARRIAGEreactiveprogramming_iot_svenbeaupr… · • Unix pipes are an example of linear flow based programming while a stream of data follows

17

FBP: NODE-RED

Page 18: REACTIVE PROGRAMMING & I T: PERFECT MARRIAGEreactiveprogramming_iot_svenbeaupr… · • Unix pipes are an example of linear flow based programming while a stream of data follows

18

A NETWORK OF CONNECTED LIGHT-WEIGHT NODES TO PROCESS STREAM(S) OF EDGE GENERATED EVENTS VIA MESSAGE PASSING

•  According to the founders: -  A light-weight, edge of network, application builder -  Easy to use, drag and drop interface -  A tool that allows the developer to focus on the task at hand

FBP: NODE-RED

Page 19: REACTIVE PROGRAMMING & I T: PERFECT MARRIAGEreactiveprogramming_iot_svenbeaupr… · • Unix pipes are an example of linear flow based programming while a stream of data follows

19

•  Messages are JSON objects

FBP: NODE-RED

“payload : “Hello world!” “topic” : “foo” … …

Page 20: REACTIVE PROGRAMMING & I T: PERFECT MARRIAGEreactiveprogramming_iot_svenbeaupr… · • Unix pipes are an example of linear flow based programming while a stream of data follows

20

•  Different categories of nodes are defined:

-  Input: to process incoming events •  Example: Inject trigger, HTTP, TCP, UDP, websocket, MQTT, file, Datastore, social

(Twitter), … and custom nodes

-  Output: to serve or send outgoing events •  Example: Debug, HTTP, TCP, UDP, websocket, MQTT, file, Datastore, social (Twitter), …

and custom nodes

-  Functions: to manipulate messages and message payloads •  Transform, filter, map, analyse, …

FBP: NODE-RED

Page 21: REACTIVE PROGRAMMING & I T: PERFECT MARRIAGEreactiveprogramming_iot_svenbeaupr… · • Unix pipes are an example of linear flow based programming while a stream of data follows

21

•  Generic Function node: -  Runs user-defined javascript code in a sandbox -  Returns (forward) custom build messages with payloads

•  return msg;

•  return [msg1, msg2, msg3];

•  return [msg1, [msgA, msgB]];

FBP: NODE-RED – FUNCTION NODE

msg

msg1

msg2

msg3

msg1

[msgA,msgB]

Page 22: REACTIVE PROGRAMMING & I T: PERFECT MARRIAGEreactiveprogramming_iot_svenbeaupr… · • Unix pipes are an example of linear flow based programming while a stream of data follows

22

•  Each node has its own context to save state between calls -  Since node.js is single threaded, no race conditions can occur and the state is

always correct and up to date

FBP: NODE-RED – FUNCTION CONTEXT

count + 1

msg

countMessage

Page 23: REACTIVE PROGRAMMING & I T: PERFECT MARRIAGEreactiveprogramming_iot_svenbeaupr… · • Unix pipes are an example of linear flow based programming while a stream of data follows

23

•  Custom nodes are defined via a pair of files: -  .js :

•  defines what the node does

•  runs on the server

-  .html : •  defines the node's properties, edit dialog and help text

•  runs in a browser

•  When configuration needs to be shared between nodes, eg. IN node and the related OUT node share the same IP address, a special config node can be defined -  A dependeny on the config node is added in the custom node

FBP: NODE-RED – CUSTOM NODES

Page 24: REACTIVE PROGRAMMING & I T: PERFECT MARRIAGEreactiveprogramming_iot_svenbeaupr… · • Unix pipes are an example of linear flow based programming while a stream of data follows

24

•  What if

(FUNCTIONAL) REACTIVE PROGRAMMING (FRP)

meant C always equals A plus B, at any time?

Page 25: REACTIVE PROGRAMMING & I T: PERFECT MARRIAGEreactiveprogramming_iot_svenbeaupr… · • Unix pipes are an example of linear flow based programming while a stream of data follows

25

•  The functions in a cell/chart react on changes in another cell

FRP: SPREADSHEETS ARE REACTIVE

Page 26: REACTIVE PROGRAMMING & I T: PERFECT MARRIAGEreactiveprogramming_iot_svenbeaupr… · • Unix pipes are an example of linear flow based programming while a stream of data follows

26

•  In reactive programming, datatypes represent a value “over time”* -  For example getting mouse coördinates

in reactive programming, the assignment needs to be made only once and the variables/properties will always be up to date with the latest values

-  All following computations based on these variables will also be values that change over time

FRP: DYNAMIC VALUES

*http://stackoverflow.com/a/1028642

Page 27: REACTIVE PROGRAMMING & I T: PERFECT MARRIAGEreactiveprogramming_iot_svenbeaupr… · • Unix pipes are an example of linear flow based programming while a stream of data follows

27

FRP: BACON.JS – HOW DOES IT LOOK LIKE?

Page 28: REACTIVE PROGRAMMING & I T: PERFECT MARRIAGEreactiveprogramming_iot_svenbeaupr… · • Unix pipes are an example of linear flow based programming while a stream of data follows

28

FRP: BACON.JS – ‘C = A + B’, ALWAYS

Page 29: REACTIVE PROGRAMMING & I T: PERFECT MARRIAGEreactiveprogramming_iot_svenbeaupr… · • Unix pipes are an example of linear flow based programming while a stream of data follows

29

•  'Functional' means that building blocks of functional programming are used -  Ideas are borrowed from languages such as Haskell, Lisp, Scheme,...

•  Higher order functions are used extensively all over the place -  Higher order = takes one or more functions as input and/or outputs a function -  Examples:

•  Map, filter, scan, combine, takeWhile,...

•  See https://github.com/baconjs/bacon.js/ for an exhaustive list supported by bacon

•  See https://github.com/ReactiveX/RxJava/wiki/Observable for visual representation of many of these functions

FRP: ‘FUNCTIONAL’ REACTIVE PROGRAMMING

Page 30: REACTIVE PROGRAMMING & I T: PERFECT MARRIAGEreactiveprogramming_iot_svenbeaupr… · • Unix pipes are an example of linear flow based programming while a stream of data follows

30

•  Transform items emitted by a stream/property by applying a function to each of them

FRP: HIGHER ORDER FUNCTION map()

https://github.com/ReactiveX/RxJava/wiki/Transforming-Observables#map

Page 31: REACTIVE PROGRAMMING & I T: PERFECT MARRIAGEreactiveprogramming_iot_svenbeaupr… · • Unix pipes are an example of linear flow based programming while a stream of data follows

31

•  Filter items emitted by a stream

FRP: HIGHER ORDER FUNCTION filter()

https://github.com/ReactiveX/RxJava/wiki/Filtering-Observables#filter

Page 32: REACTIVE PROGRAMMING & I T: PERFECT MARRIAGEreactiveprogramming_iot_svenbeaupr… · • Unix pipes are an example of linear flow based programming while a stream of data follows

32

•  Apply a function to each item emitted by a stream and emit each successive value

FRP: HIGHER ORDER FUNCTION scan()

https://github.com/ReactiveX/RxJava/wiki/Transforming-Observables#scan

Page 33: REACTIVE PROGRAMMING & I T: PERFECT MARRIAGEreactiveprogramming_iot_svenbeaupr… · • Unix pipes are an example of linear flow based programming while a stream of data follows

33

•  Emit items emitted by a stream as long as a specified condition is true, then skip the remainder

FRP: HIGHER ORDER FUNCTION takeWhile()

https://github.com/ReactiveX/RxJava/wiki/Conditional-and-Boolean-Operators#takewhile-and-takewhilewithindex

Page 34: REACTIVE PROGRAMMING & I T: PERFECT MARRIAGEreactiveprogramming_iot_svenbeaupr… · • Unix pipes are an example of linear flow based programming while a stream of data follows

34

FRP: APPLYING FRP IN IOT – BACON.JS & TESSEL

https://github.com/BareMetalJS/tessel-baconjs-examples

Page 35: REACTIVE PROGRAMMING & I T: PERFECT MARRIAGEreactiveprogramming_iot_svenbeaupr… · • Unix pipes are an example of linear flow based programming while a stream of data follows

DEMO?

Page 36: REACTIVE PROGRAMMING & I T: PERFECT MARRIAGEreactiveprogramming_iot_svenbeaupr… · • Unix pipes are an example of linear flow based programming while a stream of data follows

Q&A KEEP IN TOUCH

(don’t forget, we are looking for partners for European Funding opportunities)

[email protected] https://www.linkedin.com/in/svenbeauprez

@SvenBeauprez and @iot_bE