30
Reactive Apps with Akka and AngularJS Heiko Seeberger GeeCON 2015, Kraków

Reactive Apps with Akka and AngularJSpresentations2015.s3.amazonaws.com/61_presentation.pdfReactive Apps with Akka and AngularJS Heiko Seeberger GeeCON 2015, Kraków The Reactive Traits

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Reactive Apps with Akka and AngularJSpresentations2015.s3.amazonaws.com/61_presentation.pdfReactive Apps with Akka and AngularJS Heiko Seeberger GeeCON 2015, Kraków The Reactive Traits

Reactive Apps with Akka and AngularJS

Heiko Seeberger GeeCON 2015, Kraków

Page 2: Reactive Apps with Akka and AngularJSpresentations2015.s3.amazonaws.com/61_presentation.pdfReactive Apps with Akka and AngularJS Heiko Seeberger GeeCON 2015, Kraków The Reactive Traits

The Reactive Traits

Page 3: Reactive Apps with Akka and AngularJSpresentations2015.s3.amazonaws.com/61_presentation.pdfReactive Apps with Akka and AngularJS Heiko Seeberger GeeCON 2015, Kraków The Reactive Traits

How to go Reactive with Akka and AngularJS?

Page 4: Reactive Apps with Akka and AngularJSpresentations2015.s3.amazonaws.com/61_presentation.pdfReactive Apps with Akka and AngularJS Heiko Seeberger GeeCON 2015, Kraków The Reactive Traits

Akka

• Actors for Java/Scala

• Reactive all through

• Open Source under Apache License

“Akka is a toolkit and runtime for building highly concurrent, distributed, and resilient

message-driven applicationson the JVM.”

http://akka.io

Page 5: Reactive Apps with Akka and AngularJSpresentations2015.s3.amazonaws.com/61_presentation.pdfReactive Apps with Akka and AngularJS Heiko Seeberger GeeCON 2015, Kraków The Reactive Traits

AngularJS

• HTML/Javascript framework

• Browser-based single-page apps

• Open Source under MIT License

“HTML enhanced for web apps!”

https://angularjs.org

Page 6: Reactive Apps with Akka and AngularJSpresentations2015.s3.amazonaws.com/61_presentation.pdfReactive Apps with Akka and AngularJS Heiko Seeberger GeeCON 2015, Kraków The Reactive Traits

Demo – Reactive Flows

Page 7: Reactive Apps with Akka and AngularJSpresentations2015.s3.amazonaws.com/61_presentation.pdfReactive Apps with Akka and AngularJS Heiko Seeberger GeeCON 2015, Kraków The Reactive Traits

Demo – Reactive Flows

AngularJS

Akka HTTP Akka HTTP

Akka Data Replication

Akka Cluster Sharding

Akka Persistence

SSEREST

Page 8: Reactive Apps with Akka and AngularJSpresentations2015.s3.amazonaws.com/61_presentation.pdfReactive Apps with Akka and AngularJS Heiko Seeberger GeeCON 2015, Kraków The Reactive Traits

Demo – Reactive Flows

Page 9: Reactive Apps with Akka and AngularJSpresentations2015.s3.amazonaws.com/61_presentation.pdfReactive Apps with Akka and AngularJS Heiko Seeberger GeeCON 2015, Kraków The Reactive Traits

AngularJS Basics – Templates

<div ng-repeat="msg in messages | orderBy:'date':true"> <div> <div class="date-time">{{msg.date}}</div> <div class="text">{{msg.text}}</div> </div> </div>

Page 10: Reactive Apps with Akka and AngularJSpresentations2015.s3.amazonaws.com/61_presentation.pdfReactive Apps with Akka and AngularJS Heiko Seeberger GeeCON 2015, Kraków The Reactive Traits

AngularJS Basics – Controller

m.controller('HomeCtrl', ['$scope', function ($scope) { $scope.messages = [{ text: 'Text1', dateTime: '2015-04-14 19:20:21' }]; }]);

Page 11: Reactive Apps with Akka and AngularJSpresentations2015.s3.amazonaws.com/61_presentation.pdfReactive Apps with Akka and AngularJS Heiko Seeberger GeeCON 2015, Kraków The Reactive Traits

Demo – AngularJS Basics

Page 12: Reactive Apps with Akka and AngularJSpresentations2015.s3.amazonaws.com/61_presentation.pdfReactive Apps with Akka and AngularJS Heiko Seeberger GeeCON 2015, Kraków The Reactive Traits

Akka HTTP Basics

class HttpService extends Actor ... {

Http(context.system) .bindAndHandle(route(self), interface, port) .pipeTo(self)

... }

Page 13: Reactive Apps with Akka and AngularJSpresentations2015.s3.amazonaws.com/61_presentation.pdfReactive Apps with Akka and AngularJS Heiko Seeberger GeeCON 2015, Kraków The Reactive Traits

Akka HTTP Basics

def route(httpService: ActorRef)(...) = { def assets = getFromResourceDirectory("web") def shutdown = path("") { delete { complete { httpService ! Shutdown "Shutting down now ..." } } } assets ~ shutdown }

Page 14: Reactive Apps with Akka and AngularJSpresentations2015.s3.amazonaws.com/61_presentation.pdfReactive Apps with Akka and AngularJS Heiko Seeberger GeeCON 2015, Kraków The Reactive Traits

Demo – Akka HTTP Basics

Page 15: Reactive Apps with Akka and AngularJSpresentations2015.s3.amazonaws.com/61_presentation.pdfReactive Apps with Akka and AngularJS Heiko Seeberger GeeCON 2015, Kraków The Reactive Traits

Server-Sent Events

Page 16: Reactive Apps with Akka and AngularJSpresentations2015.s3.amazonaws.com/61_presentation.pdfReactive Apps with Akka and AngularJS Heiko Seeberger GeeCON 2015, Kraków The Reactive Traits

Server-Sent Events

en.wikipedia.org/wiki/Server-sent_events

Page 17: Reactive Apps with Akka and AngularJSpresentations2015.s3.amazonaws.com/61_presentation.pdfReactive Apps with Akka and AngularJS Heiko Seeberger GeeCON 2015, Kraków The Reactive Traits

Server-Sent Events

event:addeddata:somedata:message

ServerBrowser

text/event-stream

text/event-stream

GET /messages

Page 18: Reactive Apps with Akka and AngularJSpresentations2015.s3.amazonaws.com/61_presentation.pdfReactive Apps with Akka and AngularJS Heiko Seeberger GeeCON 2015, Kraków The Reactive Traits

Server-Sent Events – Client

var messageSource = new EventSource('/message-events'); messageSource.addEventListener( 'added', function (event) { // TODO Handle event! }, false );

Page 19: Reactive Apps with Akka and AngularJSpresentations2015.s3.amazonaws.com/61_presentation.pdfReactive Apps with Akka and AngularJS Heiko Seeberger GeeCON 2015, Kraków The Reactive Traits

Server-Sent Events – Server

path("message-events") { implicit val timeout = httpServiceTimeout get { complete { (httpService ? CreateMessageEventSource) .mapTo[ServerSentEventSource] } }}

Page 20: Reactive Apps with Akka and AngularJSpresentations2015.s3.amazonaws.com/61_presentation.pdfReactive Apps with Akka and AngularJS Heiko Seeberger GeeCON 2015, Kraków The Reactive Traits

Demo – Server-Sent Events

Page 21: Reactive Apps with Akka and AngularJSpresentations2015.s3.amazonaws.com/61_presentation.pdfReactive Apps with Akka and AngularJS Heiko Seeberger GeeCON 2015, Kraków The Reactive Traits

Conflict-Free Replicated Data Types (CRDTs)

- Consistency of replicas without central coordination?

- Certain data structures allow for eventual consistency

- State-based (CvRDTs)

- Operation-based (CmRDTs)

- Akka Data Replication offers various CvRDTs:

- Counter, Set, Map, etc.

Page 22: Reactive Apps with Akka and AngularJSpresentations2015.s3.amazonaws.com/61_presentation.pdfReactive Apps with Akka and AngularJS Heiko Seeberger GeeCON 2015, Kraków The Reactive Traits

Akka Data Replication

Replicator Replicator

Update

Gossip

Get

Page 23: Reactive Apps with Akka and AngularJSpresentations2015.s3.amazonaws.com/61_presentation.pdfReactive Apps with Akka and AngularJS Heiko Seeberger GeeCON 2015, Kraków The Reactive Traits

Demo – Akka Data Replication

Page 24: Reactive Apps with Akka and AngularJSpresentations2015.s3.amazonaws.com/61_presentation.pdfReactive Apps with Akka and AngularJS Heiko Seeberger GeeCON 2015, Kraków The Reactive Traits

Actor Sharding

- Partitioning of a (large) number of actors across a cluster

- Actors must have addressable via an identifier

- Management of subsets (shards)

- Dynamically adapt to cluster topology

Page 25: Reactive Apps with Akka and AngularJSpresentations2015.s3.amazonaws.com/61_presentation.pdfReactive Apps with Akka and AngularJS Heiko Seeberger GeeCON 2015, Kraków The Reactive Traits

Akka Cluster Sharding

ShardRegion ShardRegion

A B C

Page 26: Reactive Apps with Akka and AngularJSpresentations2015.s3.amazonaws.com/61_presentation.pdfReactive Apps with Akka and AngularJS Heiko Seeberger GeeCON 2015, Kraków The Reactive Traits

Demo – Akka Cluster Sharding

Page 27: Reactive Apps with Akka and AngularJSpresentations2015.s3.amazonaws.com/61_presentation.pdfReactive Apps with Akka and AngularJS Heiko Seeberger GeeCON 2015, Kraków The Reactive Traits

Akka Persistence

- How can we persist actor state?

- Event Sourcing:

- If received command is valid, create an event,

- let it be persisted by the Journal and

- after successful persistence, apply it to the actor state

- On recovery, events are replayed

Page 28: Reactive Apps with Akka and AngularJSpresentations2015.s3.amazonaws.com/61_presentation.pdfReactive Apps with Akka and AngularJS Heiko Seeberger GeeCON 2015, Kraków The Reactive Traits

Demo – Akka Persistence

Page 29: Reactive Apps with Akka and AngularJSpresentations2015.s3.amazonaws.com/61_presentation.pdfReactive Apps with Akka and AngularJS Heiko Seeberger GeeCON 2015, Kraków The Reactive Traits

Thanks!Follow me: @hseeberger

Code: github.com/hseeberger/reactive-flows

Page 30: Reactive Apps with Akka and AngularJSpresentations2015.s3.amazonaws.com/61_presentation.pdfReactive Apps with Akka and AngularJS Heiko Seeberger GeeCON 2015, Kraków The Reactive Traits

©Typesafe 2014 – All Rights Reserved