47
Building a system with messages and functions Sugak Alexander Sigma Software https://github.com/AlexSugak

Александр Сугак "Building a system using messages and functions"

  • Upload
    fwdays

  • View
    231

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Александр Сугак "Building a system using messages and functions"

Building a system with messages and functionsSugak AlexanderSigma Software

https://github.com/AlexSugak

Page 2: Александр Сугак "Building a system using messages and functions"

Case study: the “connected devices” project• A server accepting connections from devices,• Listening to, accumulating, filtering and

mapping devices’ state and sending updates to• A web app: SPA used to manage all connected

devices (including video streams from devices and a geo map displaying devices’ positions)

• And two types of mobile apps used to view/trigger devices state changes.

Page 3: Александр Сугак "Building a system using messages and functions"

Case study: the “connected devices” project• A server implemented in F#, managing TCP

connections to devices and using WebSockets as a transport between server and connected clients;

• F# Actors to handle connected devices state;• Reactive Extensions to manage state updates;• JS, Redux and React DOM to build web client;• JS, Redux and React Native to build two

Android mobile clients.

Page 4: Александр Сугак "Building a system using messages and functions"

Case study: the “connected devices” projectConstruction phase:• Two developers;• ~6 weeks;• 4 bugs found.

Page 5: Александр Сугак "Building a system using messages and functions"

Simplicity is a prerequisite for reliability.

Edsger W. Dijkstra (c)

Rich Hickey,“Simple Made Easy”

Page 6: Александр Сугак "Building a system using messages and functions"

Pure functions – simple possible things of programming?

Page 7: Александр Сугак "Building a system using messages and functions"

Pure functions – simple possible things of programming?

Page 8: Александр Сугак "Building a system using messages and functions"

As a developer, I want to write most of my program’s code in pure functions, so that I could easier maintain it.

Page 9: Александр Сугак "Building a system using messages and functions"

What about IO? Design using Messages!

state -> msg -> state’f(state, message)

Message

Current State New State

Page 10: Александр Сугак "Building a system using messages and functions"

What about IO? Design using Messages!

plusOne(acc, msg)

Increment!

2 3

Page 11: Александр Сугак "Building a system using messages and functions"

What about IO? Design using Messages!

f(s,m)

m

s s’ f(s,m)

m

s’’ f(s,m)

m

s’’’

Page 12: Александр Сугак "Building a system using messages and functions"

Managing state: reducing

Page 13: Александр Сугак "Building a system using messages and functions"

Managing state: reactively

Page 14: Александр Сугак "Building a system using messages and functions"

Managing state: reactive reducer

Page 15: Александр Сугак "Building a system using messages and functions"

Demo: “Telekom” dashboard

https://github.com/AlexSugak/village-internet

Page 16: Александр Сугак "Building a system using messages and functions"

Designing state

https://fsharpforfunandprofit.com/posts/designing-with-types-single-case-dus/

Page 17: Александр Сугак "Building a system using messages and functions"

Designing messages

Page 18: Александр Сугак "Building a system using messages and functions"

Designing messages

Page 19: Александр Сугак "Building a system using messages and functions"

Reducing state from events

http://ionide.io/ https://github.com/Krzysztof-Cieslak

Page 20: Александр Сугак "Building a system using messages and functions"

Receiving client messages

Page 21: Александр Сугак "Building a system using messages and functions"

Receiving client messages

Page 22: Александр Сугак "Building a system using messages and functions"

Receiving client messages

Page 23: Александр Сугак "Building a system using messages and functions"

Parsing client messages

Page 24: Александр Сугак "Building a system using messages and functions"

Validating client commands

Page 25: Александр Сугак "Building a system using messages and functions"

Endpoint Agent

Page 26: Александр Сугак "Building a system using messages and functions"

Endpoint Agent

Page 27: Александр Сугак "Building a system using messages and functions"

Endpoint Agent

Page 28: Александр Сугак "Building a system using messages and functions"

Connecting clients and endpoints

Page 29: Александр Сугак "Building a system using messages and functions"

Connecting clients and endpoints

Page 30: Александр Сугак "Building a system using messages and functions"

Connecting clients and endpoints

Page 31: Александр Сугак "Building a system using messages and functions"

Connecting clients and endpoints

Page 32: Александр Сугак "Building a system using messages and functions"

Connecting clients and endpoints

agent

Measurement!

Prev. State New State

Command!

Page 33: Александр Сугак "Building a system using messages and functions"

Sending state updates to clients

Page 34: Александр Сугак "Building a system using messages and functions"

Sending state updates to clients

Page 35: Александр Сугак "Building a system using messages and functions"

Combine latest

1

2

11

2

1

2

1

2

1

1 1

2 2

Page 36: Александр Сугак "Building a system using messages and functions"

Sending state updates to clients

Page 37: Александр Сугак "Building a system using messages and functions"

Sending state updates to clients

Page 38: Александр Сугак "Building a system using messages and functions"

Receive WebSocket connections

https://suave.io/

Page 39: Александр Сугак "Building a system using messages and functions"

Receive WebSocket connections

https://suave.io/

Page 40: Александр Сугак "Building a system using messages and functions"

React components are functions too!

ReactApp State HTML

Page 41: Александр Сугак "Building a system using messages and functions"

React components are functions too!

https://medium.com/@housecor/react-stateless-functional-components-nine-wins-you-might-have-overlooked-997b0d933dbc

Page 42: Александр Сугак "Building a system using messages and functions"

React components are functions too!

Page 43: Александр Сугак "Building a system using messages and functions"

React components are functions too!

Page 44: Александр Сугак "Building a system using messages and functions"

Rx is on the client too!

Page 45: Александр Сугак "Building a system using messages and functions"

Rx is on the client too!

Page 46: Александр Сугак "Building a system using messages and functions"

Lessons learned: key productivity drivers• Universal approaches: functional programming,

immutability, message-based design;• Universal libraries: Rx on both server and

client, React for both web and mobile;• Fast feedback: hot reload for UI development

(both web and mobile), REPL for F#.

Page 47: Александр Сугак "Building a system using messages and functions"

Thank you!