Puppet – Make stateful apps easier than stateless

30
Puppet MAKE STATEFUL APPS EASIER THAN STATELESS Marcin Warpechowski Starcounter - Sep 2015

Transcript of Puppet – Make stateful apps easier than stateless

Page 1: Puppet – Make stateful apps easier than stateless

PuppetMAKE S TATEFUL APPS EAS I ER THAN S TATELESS

Marcin WarpechowskiStarcounter - Sep 2015

Page 2: Puppet – Make stateful apps easier than stateless

What is PuppetProtocol for remote JSON data synchronization.

Uses JSON-Patch over HTTP or WebSocket,

with optional Operational Transformation.

Page 3: Puppet – Make stateful apps easier than stateless

What do we use it for?

UI

server-driven view-models

SPOILER ALERT

Page 4: Puppet – Make stateful apps easier than stateless

What is PuppetPatternProtocol for remote JSON data synchronization.

Uses JSON-Patch over HTTP or WebSocket,

with optional Operational Transformation.

Page 5: Puppet – Make stateful apps easier than stateless

What is PuppetPattern

StandardsProtocol for remote JSON data synchronization.

Uses JSON-Patch over HTTP or WebSocket,

with optional Operational Transformation.

Page 6: Puppet – Make stateful apps easier than stateless

What is PuppetPattern

Standards

OK, that’s oursbut based on a well-researched principle

Protocol for remote JSON data synchronization.

Uses JSON-Patch over HTTP or WebSocket,

with optional Operational Transformation.

Page 7: Puppet – Make stateful apps easier than stateless

Why JSON• ECMA and IETF standard (ECMA-404, RFC 7159)

• 7 kinds of values: string, number, object, array, true, false, null

• this simplicity encourages thin client

• ubiquitous (every platform supported)

Page 8: Puppet – Make stateful apps easier than stateless

Why JSON• ECMA and IETF standard (ECMA-404, RFC 7159)

• 7 kinds of values: string, number, object, array, true, false, null

• this simplicity encourages thin client

• ubiquitous (every platform supported)

Page 9: Puppet – Make stateful apps easier than stateless

Why JSON-Patch• IETF standard (RFC 6902)

• 3 major operations: add, replace, remove

• 3 helper operations: copy, move, test

• a JSON-Patch is a JSON document itself

• reduces traffic

Page 10: Puppet – Make stateful apps easier than stateless

Why JSON-Patch• 3 major operations: add, replace, remove

• 3 helper operations: copy, move, test

• a JSON-Patch is a JSON document itself

• reduces traffic

Page 11: Puppet – Make stateful apps easier than stateless

Puppet flow1. Get HTML representation of the page

2. Get JSON representation of the page

3. Send any change as JSON-Patch (WebSocket or HTTP)

4. Send clicks on links as HTTP GET type JSON-Patch

Elements in italic are not on the diagram

Page 12: Puppet – Make stateful apps easier than stateless

Puppet solves problems• communicating changes to tree-structured data between two

peers

• in our case: synchronizing UI state between the server and the client

• removes the need for glue code (a real data object is synced)

• going out of sync (Operational Transformation)

Page 13: Puppet – Make stateful apps easier than stateless

Benefits of Puppet for the UISECUR I T Y No hackable code in the client

COMPOS AB I L I T Y JSON trees can be nested

MIT IGAT ING NET WORK L ATENCY Client sends the data as you type. Server already has the data when you press OK

TEMPL AT ING S Y S TEMS Polymer, Angular, Handlebars, etc bind JSON nicely to HTML

MORPHAB I L I T Y UI can respond to changes by replacing parts of the tree

DEEP URL S Links morph the state if opened in the same tab. They reload the page only if you open a new tab

Page 14: Puppet – Make stateful apps easier than stateless

Triggering changes• when the client sends a change, this is just an intent

• it is up to the server to accept, process and react to this intent

• ...or to reject it

Page 15: Puppet – Make stateful apps easier than stateless

Triggering changes• Altering FirstName$ or LastName$ on the client

can cause the server to change the FullName

• Altering Price$ or Quantity$ on the client can cause the server to change the Total

• Pressing a button can be sent as incrementation of a number or change from false -> true

• Any change can result in morphing of the view (alteration of small or big part of JSON tree)

Page 16: Puppet – Make stateful apps easier than stateless

Why HTTP• W3C and IETF standard (multiple RFC)

• ability to ask for representation as JSON and HTML

• ability to send changes as JSON-Patch or JSON

• ability to morph documents

Page 17: Puppet – Make stateful apps easier than stateless

HTML representation

Page 18: Puppet – Make stateful apps easier than stateless

JSON representation

Page 19: Puppet – Make stateful apps easier than stateless

JSON-Patch representation

Page 20: Puppet – Make stateful apps easier than stateless

Why WebSocket• W3C and IETF standard (RFC 6455)

• messages don’t have headers

• important when message is smaller than the header

• messages delivered in order

• ability to push

Page 21: Puppet – Make stateful apps easier than stateless

Why WebSocket

Page 22: Puppet – Make stateful apps easier than stateless

Why Operational Transformation• web reality: the client and the server are doing concurrent

changes asynchronously (most of the time out-of-sync)

• goal: apply patches correctly without freezing UI or disallowing server push

• how does it work: transforms any remote operation over operations that were generated locally

Page 23: Puppet – Make stateful apps easier than stateless

Without Operational Transformation

CL I ENT SERVER

“ABCDE” “ABCDE”

“ABCE” “ACDE”

“ACD”“ACE”

C = Del 4 S = Del 2

S = Del 2 C = Del 4

T IME

Page 24: Puppet – Make stateful apps easier than stateless

With Operational Transformation

CL I ENT SERVER

T IME

“ABCDE” “ABCDE”

“ABCE” “ACDE”

“ACD”“ACE”

C = Del 4 S = Del 2

S’ = Del 2 C’ = Del 3

Page 25: Puppet – Make stateful apps easier than stateless

What Puppet is not• RPC

• Collaboration tool

Page 26: Puppet – Make stateful apps easier than stateless

What Puppet is not• RPC

• Collaboration toolBut can be used as such

Page 27: Puppet – Make stateful apps easier than stateless

Puppet implementationsJavaScript:PuppetJSFramework agnostic (tested with Polymer, Angular and plain JS)

Oriented for the role of the client (server usage samples in development)

Web Components:puppet-client Native Web Component API

Plays well with Polymer

C#:Starcounter Oriented for the role of the server

Working on releasing it as NuGet package that can be used as a server or as a client

Page 28: Puppet – Make stateful apps easier than stateless

Demo• Procurement

• trigger server change of InvoiceRow Total in Chrome console:var  o  =  document.querySelector('puppet-­‐client').obj.workspaces[0] o.Procurement.CurrentPage.Procurement.PurchaseOrderItems[0].Price$++

• Chatter

• push new Chat message from Chrome console:o.Chatter.CurrentPage.Chatter.ChatMessage.Text$  =  "Hi  from  console!” o.Chatter.CurrentPage.Chatter.Send$++

Page 29: Puppet – Make stateful apps easier than stateless

Future topics• Puppet does not replace REST, it enhances it

• Puppet shares some mindset with DDP, Falcor, ShareJS

• Detailed view into OT

Page 30: Puppet – Make stateful apps easier than stateless

GitHub: https://github.com/puppetjs/puppetjs (see Wiki)

Twitter: @starcounterdb, @warpech

Thanks! More info on

.io