Slide 1

Slide 1 text

Taming WebSocket with Scarlet Zhixuan Lai @zhxnlai github.com/zhxnlai

Slide 2

Slide 2 text

WebSocket

Slide 3

Slide 3 text

WebSocket • Supports text and binary messages

Slide 4

Slide 4 text

WebSocket • Supports text and binary messages • Realtime

Slide 5

Slide 5 text

WebSocket • Supports text and binary messages • Realtime • Efficient

Slide 6

Slide 6 text

WebSocket • Supports text and binary messages • Realtime • Efficient • Bidirectional

Slide 7

Slide 7 text

WebSocket

Slide 8

Slide 8 text

WebSocket // Create WebSocket connection. const socket = new WebSocket('ws://localhost:8080'); // Connection opened socket.addEventListener('open', function (event) { socket.send('Hello Server!'); }); // Listen for messages socket.addEventListener('message', function (event) { console.log('Message from server', data); });

Slide 9

Slide 9 text

WebSocket // Create WebSocket connection. const socket = new WebSocket('ws://localhost:8080'); // Connection opened socket.addEventListener('open', function (event) { socket.send('Hello Server!'); }); // Listen for messages socket.addEventListener('message', function (event) { console.log('Message from server', data); });

Slide 10

Slide 10 text

WebSocket // Create WebSocket connection. const socket = new WebSocket('ws://localhost:8080'); // Connection opened socket.addEventListener('open', function (event) { socket.send('Hello Server!'); }); // Listen for messages socket.addEventListener('message', function (event) { console.log('Message from server', data); });

Slide 11

Slide 11 text

WebSocket // Create WebSocket connection. const socket = new WebSocket('ws://localhost:8080'); // Connection opened socket.addEventListener('open', function (event) { socket.send('Hello Server!'); }); // Listen for messages socket.addEventListener('message', function (event) { console.log('Message from server', data); });

Slide 12

Slide 12 text

WebSocket // Create WebSocket connection. const socket = new WebSocket('ws://localhost:8080'); // Connection opened socket.addEventListener('open', function (event) { socket.send('Hello Server!'); }); // Listen for messages socket.addEventListener('message', function (event) { console.log('Message from server’, event.data); });

Slide 13

Slide 13 text

Use Cases

Slide 14

Slide 14 text

Use Cases • Chat • Multiplayer Games • Realtime Feed

Slide 15

Slide 15 text

WebSocket vs REST

Slide 16

Slide 16 text

WebSocket vs REST • Do we need it?

Slide 17

Slide 17 text

WebSocket vs REST • Bitcoin trading

Slide 18

Slide 18 text

Bitcoin Trading

Slide 19

Slide 19 text

Bitcoin Trading • Buy low and sell high

Slide 20

Slide 20 text

Bitcoin Trading • Buy low and sell high • React to market movement

Slide 21

Slide 21 text

Bitcoin Trading • Buy low and sell high • React to market movement • Realtime bitcoin price

Slide 22

Slide 22 text

Bitcoin Trading

Slide 23

Slide 23 text

data class Ticker( val time: String, val price: String ) Bitcoin Trading

Slide 24

Slide 24 text

data class Ticker( val time: String, val price: String ) Bitcoin Trading

Slide 25

Slide 25 text

GDAX Ticker

Slide 26

Slide 26 text

GDAX Ticker • REST API • WebSocket API

Slide 27

Slide 27 text

GDAX Ticker REST API

Slide 28

Slide 28 text

GDAX Ticker REST API interface GdaxRESTApi { @GET(“/products/{product-id}/ticker") ): Flowable } val gdaxRESTApi = retrofit.create()

Slide 29

Slide 29 text

GDAX Ticker REST API interface GdaxRESTApi { @GET(“/products/{product-id}/ticker") ): Flowable } val gdaxRESTApi = retrofit.create()

Slide 30

Slide 30 text

GDAX Ticker REST API interface GdaxRESTApi { } val gdaxRESTApi = retrofit.create()

Slide 31

Slide 31 text

GDAX Ticker REST API interface GdaxRESTApi { @GET("/products/{product-id}/ticker") fun getTicker(): Flowable } val gdaxRESTApi = retrofit.create()

Slide 32

Slide 32 text

GDAX Ticker REST API interface GdaxRESTApi { @GET("/products/BTC-USD/ticker") fun getTicker(): Flowable } val gdaxRESTApi = retrofit.create()

Slide 33

Slide 33 text

GDAX Ticker REST API interface GdaxRESTApi { @GET("/products/BTC-USD/ticker") fun getTicker(): Flowable } val gdaxRESTApi = retrofit.create()

Slide 34

Slide 34 text

GDAX Ticker REST API interface GdaxRESTApi { @GET("/products/BTC-USD/ticker") fun getTicker(): Flowable } val gdaxRESTApi = retrofit.create() gdaxRESTApi.getTicker() .subscribe { Log.d("Bitcoin price is ${ticker.price} at ${ticker.time}") }

Slide 35

Slide 35 text

GDAX Ticker REST API interface GdaxRESTApi { @GET("/products/BTC-USD/ticker") fun getTicker(): Flowable } val gdaxRESTApi = retrofit.create() gdaxRESTApi.getTicker() .subscribe { ticker -> Log.d("Bitcoin price is \$${ticker.price} at ${ticker.time}”) }

Slide 36

Slide 36 text

GDAX Ticker REST API interface GdaxRESTApi { @GET("/products/BTC-USD/ticker") fun getTicker(): Flowable } val gdaxRESTApi = retrofit.create() gdaxRESTApi.getTicker() .subscribe { ticker -> Log.d("Bitcoin price is \$${ticker.price} at ${ticker.time}”) }

Slide 37

Slide 37 text

GDAX Ticker REST API interface GdaxRESTApi { @GET("/products/BTC-USD/ticker") fun getTicker(): Flowable } val gdaxRESTApi = retrofit.create() gdaxRESTApi.getTicker() .subscribe { ticker -> Log.d("Bitcoin price is \$${ticker.price} at ${ticker.time}”) } // "Bitcoin price is $7600.01 at 2018-06-09T16:46:57.458Z”

Slide 38

Slide 38 text

GDAX Ticker REST API

Slide 39

Slide 39 text

GDAX Ticker REST API • Polling

Slide 40

Slide 40 text

Polling GDAX Ticker

Slide 41

Slide 41 text

Polling GDAX Ticker • Delay

Slide 42

Slide 42 text

Polling GDAX Ticker • Delay • Overhead

Slide 43

Slide 43 text

Polling GDAX Ticker • Delay • Overhead • Wasteful

Slide 44

Slide 44 text

GDAX Ticker WebSocket API

Slide 45

Slide 45 text

GDAX Ticker WebSocket API • Pushing • Realtime • Efficient

Slide 46

Slide 46 text

What’s wrong with WebSocket?

Slide 47

Slide 47 text

What’s wrong with WebSocket? • Difficult to set up

Slide 48

Slide 48 text

What’s wrong with WebSocket? • Difficult to set up • Boilerplate code

Slide 49

Slide 49 text

What’s wrong with WebSocket? • Difficult to set up • Boilerplate code • State management headache

Slide 50

Slide 50 text

What’s wrong with WebSocket? • Difficult to set up • Boilerplate code • State management headache • Difficult to maintain

Slide 51

Slide 51 text

What’s wrong with WebSocket? • Difficult to set up • Boilerplate code • State management headache • Difficult to maintain • Server API changes

Slide 52

Slide 52 text

What’s wrong with WebSocket? • Difficult to set up • Boilerplate code • State management headache • Difficult to maintain • Server API changes • Tech stack migration

Slide 53

Slide 53 text

Why Scarlet?

Slide 54

Slide 54 text

Why Scarlet? • Easy to get started

Slide 55

Slide 55 text

Why Scarlet? • Easy to get started • Easy to maintain

Slide 56

Slide 56 text

Why Scarlet? • Easy to get started • Easy to maintain • Reliable

Slide 57

Slide 57 text

Getting Started with Scarlet • GDAX WebSocket API

Slide 58

Slide 58 text

GDAX WebSocket API

Slide 59

Slide 59 text

GDAX WebSocket API • REST: request and response

Slide 60

Slide 60 text

GDAX WebSocket API • REST: request and response • WebSocket: bidirectional messages

Slide 61

Slide 61 text

GDAX WebSocket API • REST: request and response • WebSocket: bidirectional messages • Send Subscribe on connection open to stream Ticker

Slide 62

Slide 62 text

WebSocket with Scarlet

Slide 63

Slide 63 text

WebSocket with Scarlet interface GdaxWebSocketService { @Send fun sendSubscribe(subscribe: Subscribe) @Receive fun observeTicker(): Flowable } val gdaxService = scarlet.create()

Slide 64

Slide 64 text

WebSocket with Scarlet interface GdaxWebSocketService { @Send fun sendSubscribe(subscribe: Subscribe) @Receive fun observeTicker(): Flowable } val gdaxService = scarlet.create()

Slide 65

Slide 65 text

WebSocket with Scarlet interface GdaxWebSocketService { @Send fun sendSubscribe(subscribe: Subscribe) @Receive fun observeTicker(): Flowable } val gdaxService = scarlet.create()

Slide 66

Slide 66 text

WebSocket with Scarlet interface GdaxWebSocketService { @Send fun sendSubscribe(subscribe: Subscribe) @Receive fun observeTicker(): Flowable } val gdaxService = scarlet.create()

Slide 67

Slide 67 text

WebSocket with Scarlet interface GdaxWebSocketService { @Send fun sendSubscribe(subscribe: Subscribe) @Receive fun observeTicker(): Flowable } val gdaxService = scarlet.create()

Slide 68

Slide 68 text

WebSocket with Scarlet interface GdaxWebSocketService { @Send fun sendSubscribe(subscribe: Subscribe) @Receive fun observeTicker(): Flowable } val gdaxService = scarlet.create()

Slide 69

Slide 69 text

WebSocket with Scarlet interface GdaxWebSocketService { @Send fun sendSubscribe(subscribe: Subscribe) // ... } data class Subscribe( val type: String = "subscribe", @Json(name = "product_ids") val productIds: List, val channels: List )

Slide 70

Slide 70 text

WebSocket with Scarlet interface GdaxWebSocketService { @Send fun sendSubscribe(subscribe: Subscribe) // ... } data class Subscribe( val type: String, val productIds: List, val channels: List )

Slide 71

Slide 71 text

WebSocket with Scarlet interface GdaxWebSocketService { @Send fun sendSubscribe(subscribe: Subscribe) // ... } data class Subscribe( // ... )

Slide 72

Slide 72 text

WebSocket with Scarlet interface GdaxWebSocketService { @Send fun sendSubscribe(subscribe: Subscribe) // ... } data class Subscribe( // ... ) val BITCOIN_TICKER_SUBSCRIBE_MESSAGE = Subscribe( type = "subscribe", productIds = listOf("BTC-USD"), channels = listOf("ticker") )

Slide 73

Slide 73 text

WebSocket with Scarlet interface GdaxWebSocketService { @Send fun sendSubscribe(subscribe: Subscribe) // ... } data class Subscribe( // ... ) val BITCOIN_TICKER_SUBSCRIBE_MESSAGE = Subscribe( type = "subscribe", productIds = listOf("BTC-USD"), channels = listOf("ticker") )

Slide 74

Slide 74 text

WebSocket with Scarlet interface GdaxWebSocketService { @Send fun sendSubscribe(subscribe: Subscribe) // ... } data class Subscribe( // ... ) val BITCOIN_TICKER_SUBSCRIBE_MESSAGE = // ...

Slide 75

Slide 75 text

WebSocket with Scarlet interface GdaxWebSocketService { @Send fun sendSubscribe(subscribe: Subscribe) // ... } data class Subscribe( // ... ) val BITCOIN_TICKER_SUBSCRIBE_MESSAGE = // ... val gdaxService = scarlet.create()

Slide 76

Slide 76 text

WebSocket with Scarlet interface GdaxWebSocketService { @Send fun sendSubscribe(subscribe: Subscribe) // ... } data class Subscribe( // ... ) val BITCOIN_TICKER_SUBSCRIBE_MESSAGE = // ... val gdaxService = // ...

Slide 77

Slide 77 text

WebSocket with Scarlet interface GdaxWebSocketService { @Send fun sendSubscribe(subscribe: Subscribe) // ... } data class Subscribe( // ... ) val BITCOIN_TICKER_SUBSCRIBE_MESSAGE = // ... val gdaxService = // ... gdaxService.sendSubscribe(BITCOIN_TICKER_SUBSCRIBE_MESSAGE)

Slide 78

Slide 78 text

WebSocket with Scarlet interface GdaxWebSocketService { @Send fun sendSubscribe(subscribe: Subscribe) // ... } data class Subscribe( // ... ) val BITCOIN_TICKER_SUBSCRIBE_MESSAGE = // ... val gdaxService = // ... gdaxService.sendSubscribe(BITCOIN_TICKER_SUBSCRIBE_MESSAGE)

Slide 79

Slide 79 text

WebSocket with Scarlet interface GdaxWebSocketService { @Send fun sendSubscribe(subscribe: Subscribe) // ... }

Slide 80

Slide 80 text

WebSocket with Scarlet interface GdaxWebSocketService { // ... }

Slide 81

Slide 81 text

WebSocket with Scarlet interface GdaxWebSocketService { // ... @Receive fun observeOnConnectionOpenedEvent( ): Flowable> }

Slide 82

Slide 82 text

WebSocket with Scarlet interface GdaxWebSocketService { // ... @Receive fun observeOnConnectionOpenedEvent( ): Flowable> }

Slide 83

Slide 83 text

WebSocket with Scarlet interface GdaxWebSocketService { // ... @Receive fun observeOnConnectionOpenedEvent( ): Flowable> }

Slide 84

Slide 84 text

WebSocket with Scarlet interface GdaxWebSocketService { // ... @Receive fun observeOnConnectionOpenedEvent( ): Flowable> } gdaxService.observeOnConnectionOpenedEvent() .subscribe { gdaxService.sendSubscribe(BITCOIN_TICKER_SUBSCRIBE_MESSAGE) }

Slide 85

Slide 85 text

WebSocket with Scarlet interface GdaxWebSocketService { // ... @Receive fun observeOnConnectionOpenedEvent( ): Flowable> } gdaxService.observeOnConnectionOpenedEvent() .subscribe { gdaxService.sendSubscribe(BITCOIN_TICKER_SUBSCRIBE_MESSAGE) }

Slide 86

Slide 86 text

WebSocket with Scarlet interface GdaxWebSocketService { // ... @Receive fun observeOnConnectionOpenedEvent( ): Flowable> } gdaxService.observeOnConnectionOpenedEvent() .subscribe { gdaxService.sendSubscribe(BITCOIN_TICKER_SUBSCRIBE_MESSAGE) }

Slide 87

Slide 87 text

WebSocket with Scarlet interface GdaxWebSocketService { // ... @Receive fun observeOnConnectionOpenedEvent( ): Flowable> }

Slide 88

Slide 88 text

WebSocket with Scarlet interface GdaxWebSocketService { // ... }

Slide 89

Slide 89 text

WebSocket with Scarlet interface GdaxWebSocketService { // ... @Receive fun observeTicker(): Flowable }

Slide 90

Slide 90 text

WebSocket with Scarlet interface GdaxWebSocketService { // ... @Receive fun observeTicker(): Flowable }

Slide 91

Slide 91 text

WebSocket with Scarlet interface GdaxWebSocketService { // ... @Receive fun observeTicker(): Flowable }

Slide 92

Slide 92 text

WebSocket with Scarlet interface GdaxWebSocketService { // ... @Receive fun observeTicker(): Flowable } gdaxService.observeTicker() .subscribe { ticker -> Log.d("Bitcoin price is ${ticker.price} at ${ticker.time}") }

Slide 93

Slide 93 text

WebSocket with Scarlet interface GdaxWebSocketService { // ... @Receive fun observeTicker(): Flowable } gdaxService.observeTicker() .subscribe { ticker -> Log.d("Bitcoin price is ${ticker.price} at ${ticker.time}") }

Slide 94

Slide 94 text

WebSocket with Scarlet interface GdaxWebSocketService { // ... @Receive fun observeTicker(): Flowable } gdaxService.observeTicker() .subscribe { ticker -> Log.d("Bitcoin price is \$${ticker.price} at ${ticker.time}”) }

Slide 95

Slide 95 text

WebSocket with Scarlet interface GdaxWebSocketService { // ... @Receive fun observeTicker(): Flowable } gdaxService.observeTicker() .subscribe { ticker -> Log.d("Bitcoin price is \$${ticker.price} at ${ticker.time}”) } // "Bitcoin price is $7600.01 at 2018-06-09T16:46:57.458Z"

Slide 96

Slide 96 text

WebSocket with Scarlet interface GdaxWebSocketService { // ... @Receive fun observeTicker(): Flowable } gdaxService.observeTicker() .subscribe { ticker -> Log.d("Bitcoin price is \$${ticker.price} at ${ticker.time}”) } // "Bitcoin price is $7600.01 at 2018-06-09T16:46:57.458Z" // "Bitcoin price is $7600.85 at 2018-06-09T16:46:58.033Z"

Slide 97

Slide 97 text

WebSocket with Scarlet interface GdaxWebSocketService { // ... @Receive fun observeTicker(): Flowable } gdaxService.observeTicker() .subscribe { ticker -> Log.d("Bitcoin price is \$${ticker.price} at ${ticker.time}”) } // "Bitcoin price is $7600.01 at 2018-06-09T16:46:57.458Z" // "Bitcoin price is $7600.85 at 2018-06-09T16:46:58.033Z" // "Bitcoin price is $7601.62 at 2018-06-09T16:46:58.610Z”

Slide 98

Slide 98 text

WebSocket with Scarlet interface GdaxWebSocketService { // ... @Receive fun observeTicker(): Flowable }

Slide 99

Slide 99 text

WebSocket with Scarlet interface GdaxWebSocketService { // ... }

Slide 100

Slide 100 text

WebSocket with Scarlet interface GdaxWebSocketService { @Send fun sendSubscribe(subscribe: Subscribe) @Receive fun observeOnConnectionOpenedEvent( ): Flowable> @Receive fun observeTicker(): Flowable }

Slide 101

Slide 101 text

WebSocket with Scarlet interface GdaxWebSocketService { @Send fun sendSubscribe(subscribe: Subscribe) @Receive fun observeOnConnectionOpenedEvent( ): Flowable> @Receive fun observeTicker(): Flowable }

Slide 102

Slide 102 text

WebSocket with Scarlet interface GdaxWebSocketService { @Send fun sendSubscribe(subscribe: Subscribe) @Receive fun observeOnConnectionOpenedEvent( ): Flowable> @Receive fun observeTicker(): Flowable } val gdaxService = scarlet.create() gdaxService.observeOnConnectionOpenedEvent() .subscribe { gdaxService.sendSubscribe(BITCOIN_TICKER_SUBSCRIBE_MESSAGE) } gdaxService.observeTicker() .subscribe { ticker -> Log.d("Bitcoin price is ${ticker.price} at ${ticker.time}") }

Slide 103

Slide 103 text

WebSocket with Scarlet interface GdaxWebSocketService { @Send fun sendSubscribe(subscribe: Subscribe) @Receive fun observeOnConnectionOpenedEvent( ): Flowable> @Receive fun observeTicker(): Flowable } val gdaxService = // ... gdaxService.observeOnConnectionOpenedEvent() .subscribe { gdaxService.sendSubscribe(BITCOIN_TICKER_SUBSCRIBE_MESSAGE) } gdaxService.observeTicker() .subscribe { ticker -> Log.d("Bitcoin price is ${ticker.price} at ${ticker.time}") }

Slide 104

Slide 104 text

WebSocket with Scarlet interface GdaxWebSocketService { @Send fun sendSubscribe(subscribe: Subscribe) @Receive fun observeOnConnectionOpenedEvent( ): Flowable> @Receive fun observeTicker(): Flowable } val gdaxService = // ... gdaxService.observeOnConnectionOpenedEvent() .subscribe { gdaxService.sendSubscribe(BITCOIN_TICKER_SUBSCRIBE_MESSAGE) } gdaxService.observeTicker() .subscribe { ticker -> Log.d("Bitcoin price is ${ticker.price} at ${ticker.time}") }

Slide 105

Slide 105 text

WebSocket with Scarlet interface GdaxWebSocketService { @Send fun sendSubscribe(subscribe: Subscribe) @Receive fun observeOnConnectionOpenedEvent( ): Flowable> @Receive fun observeTicker(): Flowable } val gdaxService = // ... gdaxService.observeOnConnectionOpenedEvent() .subscribe { gdaxService.sendSubscribe(BITCOIN_TICKER_SUBSCRIBE_MESSAGE) } gdaxService.observeTicker() .subscribe { ticker -> Log.d("Bitcoin price is ${ticker.price} at ${ticker.time}") }

Slide 106

Slide 106 text

WebSocket with Scarlet interface GdaxWebSocketService { @Send fun sendSubscribe(subscribe: Subscribe) @Receive fun observeOnConnectionOpenedEvent( ): Flowable> @Receive fun observeTicker(): Flowable } val gdaxService = // ... gdaxService.observeOnConnectionOpenedEvent() .subscribe { gdaxService.sendSubscribe(BITCOIN_TICKER_SUBSCRIBE_MESSAGE) } gdaxService.observeTicker() .subscribe { ticker -> Log.d("Bitcoin price is ${ticker.price} at ${ticker.time}") }

Slide 107

Slide 107 text

WebSocket with Scarlet interface GdaxWebSocketService { @Send fun sendSubscribe(subscribe: Subscribe) @Receive fun observeOnConnectionOpenedEvent( ): Flowable> @Receive fun observeTicker(): Flowable } val gdaxService = // ... gdaxService.observeOnConnectionOpenedEvent() .subscribe { gdaxService.sendSubscribe(BITCOIN_TICKER_SUBSCRIBE_MESSAGE) } gdaxService.observeTicker() .subscribe { ticker -> Log.d("Bitcoin price is ${ticker.price} at ${ticker.time}") }

Slide 108

Slide 108 text

Scarlet Plugins

Slide 109

Slide 109 text

Scarlet Plugins • Built-in with OkHttp, Moshi, Gson, Protobuf, RxJava

Slide 110

Slide 110 text

Scarlet Plugins • Built-in with OkHttp, Moshi, Gson, Protobuf, RxJava • Roadmap: Jackson, xml, LiveData, Java 9 Flow, and more

Slide 111

Slide 111 text

Scarlet Plugins • Built-in with OkHttp, Moshi, Gson, Protobuf, RxJava • Roadmap: Jackson, xml, LiveData, Java 9 Flow, and more • Fully customizable

Slide 112

Slide 112 text

Scarlet Plugins const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 113

Slide 113 text

Scarlet Plugins const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 114

Slide 114 text

Scarlet Plugins const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 115

Slide 115 text

Scarlet Plugins const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 116

Slide 116 text

Scarlet Plugins const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 117

Slide 117 text

Scarlet Plugins const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 118

Slide 118 text

Scarlet Plugins const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 119

Slide 119 text

Scarlet Plugins const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 120

Slide 120 text

Scarlet Plugins const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 121

Slide 121 text

Scarlet Plugins const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 122

Slide 122 text

Scarlet Plugins const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 123

Slide 123 text

Scarlet Plugins const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 124

Slide 124 text

Scarlet Plugins const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 125

Slide 125 text

WebSocketFactory

Slide 126

Slide 126 text

WebSocketFactory • Supports any WebSocket implementation

Slide 127

Slide 127 text

WebSocketFactory const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 128

Slide 128 text

WebSocketFactory const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(MockHttpServer().newWebSocketFactory()) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 129

Slide 129 text

WebSocketFactory const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(CustomWebSocketFactory()) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 130

Slide 130 text

WebSocketFactory (Roadmap)

Slide 131

Slide 131 text

WebSocketFactory (Roadmap) •java-websocket •java.net.socket

Slide 132

Slide 132 text

Scarlet Plugins const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(CustomWebSocketFactory()) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 133

Slide 133 text

Scarlet Plugins const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(CustomWebSocketFactory()) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 134

Slide 134 text

MessageAdapterFactory

Slide 135

Slide 135 text

MessageAdapterFactory • Text and binary messages

Slide 136

Slide 136 text

MessageAdapterFactory • Text and binary messages • Serialization & deserialization of any type •Subscribe •Ticker

Slide 137

Slide 137 text

MessageAdapterFactory const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 138

Slide 138 text

MessageAdapterFactory const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(GsonMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 139

Slide 139 text

MessageAdapterFactory const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(ProtobufMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 140

Slide 140 text

MessageAdapterFactory (Roadmap) const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(JacksonMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 141

Slide 141 text

MessageAdapterFactory (Roadmap) const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(XmlMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 142

Slide 142 text

Scarlet Plugins const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(XmlMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 143

Slide 143 text

Scarlet Plugins const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(XmlMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 144

Slide 144 text

StreamAdapter

Slide 145

Slide 145 text

StreamAdapter • Stream • Sequence of asynchronous values

Slide 146

Slide 146 text

StreamAdapter • Stream • Sequence of asynchronous values • Scarlet Stream

Slide 147

Slide 147 text

StreamAdapter • Stream • Sequence of asynchronous values • Scarlet Stream • Converts Scarlet Stream into other abstractions

Slide 148

Slide 148 text

StreamAdapterFactory const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 149

Slide 149 text

StreamAdapterFactory const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava1StreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 150

Slide 150 text

StreamAdapterFactory (Roadmap) const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(LiveDataStreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 151

Slide 151 text

StreamAdapterFactory (Roadmap) const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(Java9FlowStreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 152

Slide 152 text

StreamAdapterFactory (Roadmap) const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(KotlinCoroutineStreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 153

Slide 153 text

Scarlet Plugins const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(KotlinCoroutineStreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 154

Slide 154 text

Maintaining Scarlet

Slide 155

Slide 155 text

Maintaining Scarlet • Easy to maintain

Slide 156

Slide 156 text

Supporting API Changes • Receiving Level 2

Slide 157

Slide 157 text

Supporting API Changes { "type": "snapshot", "product_id": "BTC-USD", "bids": [["6500.11", "0.45054140"]], "asks": [["6500.15", "0.57753524"]] }

Slide 158

Slide 158 text

Supporting API Changes • Receiving Level 2

Slide 159

Slide 159 text

Supporting API Changes • Receiving Level 2 • 2 loc

Slide 160

Slide 160 text

Supporting API Changes interface GdaxWebSocketService { @Send fun sendSubscribe(subscribe: Subscribe) @Receive fun observeOnConnectionOpenedEvent( ): Flowable> @Receive fun observeTicker(): Flowable @Receive fun observeLevel2(): Flowable } val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 161

Slide 161 text

Supporting API Changes interface GdaxWebSocketService { @Send fun sendSubscribe(subscribe: Subscribe) @Receive fun observeOnConnectionOpenedEvent( ): Flowable> @Receive fun observeTicker(): Flowable @Receive fun observeLevel2(): Flowable } val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 162

Slide 162 text

Changing WebSocket Implementation

Slide 163

Slide 163 text

Changing WebSocket Implementation • Use a custom WebSocket implementation

Slide 164

Slide 164 text

Changing WebSocket Implementation • Use a custom WebSocket implementation • 1 loc

Slide 165

Slide 165 text

Changing WebSocket Implementation interface GdaxWebSocketService { @Send fun sendSubscribe(subscribe: Subscribe) @Receive fun observeOnConnectionOpenedEvent( ): Flowable> @Receive fun observeTicker(): Flowable @Receive fun observeLevel2(): Flowable } val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 166

Slide 166 text

Changing WebSocket Implementation interface GdaxWebSocketService { @Send fun sendSubscribe(subscribe: Subscribe) @Receive fun observeOnConnectionOpenedEvent( ): Flowable> @Receive fun observeTicker(): Flowable @Receive fun observeLevel2(): Flowable } val scarlet = Scarlet.Builder() .webSocketFactory(CustomWebSocketFactory()) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 167

Slide 167 text

Changing JSON Library

Slide 168

Slide 168 text

Changing JSON Library • From Moshi to Jackson

Slide 169

Slide 169 text

Changing JSON Library • From Moshi to Jackson • 1 loc

Slide 170

Slide 170 text

Changing JSON Library interface GdaxWebSocketService { @Send fun sendSubscribe(subscribe: Subscribe) @Receive fun observeOnConnectionOpenedEvent( ): Flowable> @Receive fun observeTicker(): Flowable @Receive fun observeLevel2(): Flowable } val scarlet = Scarlet.Builder() .webSocketFactory(CustomWebSocketFactory()) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 171

Slide 171 text

Changing JSON Library interface GdaxWebSocketService { @Send fun sendSubscribe(subscribe: Subscribe) @Receive fun observeOnConnectionOpenedEvent( ): Flowable> @Receive fun observeTicker(): Flowable @Receive fun observeLevel2(): Flowable } val scarlet = Scarlet.Builder() .webSocketFactory(CustomWebSocketFactory()) .addMessageAdapterFactory(JacksonMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 172

Slide 172 text

Supporting Kotlin Coroutine

Slide 173

Slide 173 text

Supporting Kotlin Coroutine • From Flowable to Sequence

Slide 174

Slide 174 text

Supporting Kotlin Coroutine • From Flowable to Sequence • 3 loc

Slide 175

Slide 175 text

Supporting Kotlin Coroutine interface GdaxWebSocketService { @Send fun sendSubscribe(subscribe: Subscribe) @Receive fun observeOnConnectionOpenedEvent( ): Flowable> @Receive fun observeTicker(): Flowable @Receive fun observeLevel2(): Flowable } val scarlet = Scarlet.Builder() .webSocketFactory(CustomWebSocketFactory()) .addMessageAdapterFactory(JacksonMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 176

Slide 176 text

Supporting Kotlin Coroutine interface GdaxWebSocketService { @Send fun sendSubscribe(subscribe: Subscribe) @Receive fun observeOnConnectionOpenedEvent( ): Flowable> @Receive fun observeTicker(): Sequence @Receive fun observeLevel2(): Sequence } val scarlet = Scarlet.Builder() .webSocketFactory(CustomWebSocketFactory()) .addMessageAdapterFactory(JacksonMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 177

Slide 177 text

Supporting Kotlin Coroutine interface GdaxWebSocketService { @Send fun sendSubscribe(subscribe: Subscribe) @Receive fun observeOnConnectionOpenedEvent( ): Flowable> @Receive fun observeTicker(): Sequence @Receive fun observeLevel2(): Sequence } val scarlet = Scarlet.Builder() .webSocketFactory(CustomWebSocketFactory()) .addMessageAdapterFactory(JacksonMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 178

Slide 178 text

Supporting Kotlin Coroutine interface GdaxWebSocketService { @Send fun sendSubscribe(subscribe: Subscribe) @Receive fun observeOnConnectionOpenedEvent( ): Flowable> @Receive fun observeTicker(): Sequence @Receive fun observeLevel2(): Sequence } val scarlet = Scarlet.Builder() .webSocketFactory(CustomWebSocketFactory()) .addMessageAdapterFactory(JacksonMessageAdapter.Factory()) .addStreamAdapterFactory(KotlinCoroutineStreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 179

Slide 179 text

Supporting Kotlin Coroutine interface GdaxWebSocketService { @Send fun sendSubscribe(subscribe: Subscribe) @Receive fun observeOnConnectionOpenedEvent( ): Flowable> @Receive fun observeTicker(): Sequence @Receive fun observeLevel2(): Sequence } val scarlet = Scarlet.Builder() .webSocketFactory(CustomWebSocketFactory()) .addMessageAdapterFactory(JacksonMessageAdapter.Factory()) .addStreamAdapterFactory(KotlinCoroutineStreamAdapter.Factory()) .build() val gdaxService = scarlet.create()

Slide 180

Slide 180 text

Scarlet Reliability

Slide 181

Slide 181 text

Scarlet Reliability • Tested in production

Slide 182

Slide 182 text

Scarlet Reliability • Tested in production • 190 countries

Slide 183

Slide 183 text

Scarlet Reliability • Tested in production • 190 countries • 26 million matches every day

Slide 184

Slide 184 text

Scarlet Reliability • Tested in production • 190 countries • 26 million matches every day • Thousands of devices

Slide 185

Slide 185 text

WebSocket on Android

Slide 186

Slide 186 text

WebSocket on Android • Straightforward when the connection is open

Slide 187

Slide 187 text

WebSocket on Android • Straightforward when the connection is open • But a connection may fail

Slide 188

Slide 188 text

WebSocket on Android

Slide 189

Slide 189 text

WebSocket on Android • Unstable network

Slide 190

Slide 190 text

WebSocket on Android • Unstable network • Server closure

Slide 191

Slide 191 text

WebSocket on Android • Unstable network • Server closure • App enters background

Slide 192

Slide 192 text

WebSocket on Android

Slide 193

Slide 193 text

WebSocket on Android

Slide 194

Slide 194 text

WebSocket on Android

Slide 195

Slide 195 text

WebSocket on Android

Slide 196

Slide 196 text

WebSocket on Android

Slide 197

Slide 197 text

WebSocket on Android

Slide 198

Slide 198 text

WebSocket on Android

Slide 199

Slide 199 text

WebSocket on Android

Slide 200

Slide 200 text

WebSocket on Android

Slide 201

Slide 201 text

WebSocket on Android

Slide 202

Slide 202 text

WebSocket on Android

Slide 203

Slide 203 text

WebSocket on Android

Slide 204

Slide 204 text

WebSocket on Android

Slide 205

Slide 205 text

WebSocket on Android

Slide 206

Slide 206 text

State Management in Scarlet

Slide 207

Slide 207 text

State Management in Scarlet • Scarlet has got you covered

Slide 208

Slide 208 text

State Management in Scarlet

Slide 209

Slide 209 text

State Management in Scarlet • Reliable

Slide 210

Slide 210 text

State Management in Scarlet • Reliable • Testable and debuggable

Slide 211

Slide 211 text

State Management in Scarlet • Reliable • Testable and debuggable • Fully tested

Slide 212

Slide 212 text

What makes Scarlet testable?

Slide 213

Slide 213 text

What makes Scarlet testable? • Immutable State, Event and Transition • State Machine DSL

Slide 214

Slide 214 text

State Machine DSL

Slide 215

Slide 215 text

State Machine DSL sealed class State { object Solid : State() object Liquid : State() object Gas : State() } sealed class Event { object OnMelted : Event() object OnFroze : Event() object OnVaporized : Event() object OnCondensed : Event() }

Slide 216

Slide 216 text

State Machine DSL sealed class State { object Solid : State() object Liquid : State() object Gas : State() } sealed class Event { object OnMelted : Event() object OnFroze : Event() object OnVaporized : Event() object OnCondensed : Event() }

Slide 217

Slide 217 text

State Machine DSL sealed class State { object Solid : State() object Liquid : State() object Gas : State() } sealed class Event { object OnMelted : Event() object OnFroze : Event() object OnVaporized : Event() object OnCondensed : Event() }

Slide 218

Slide 218 text

State Machine DSL sealed class State { object Solid : State() object Liquid : State() object Gas : State() } sealed class Event { object OnMelted : Event() object OnFroze : Event() object OnVaporized : Event() object OnCondensed : Event() }

Slide 219

Slide 219 text

State Machine DSL sealed class State { object Solid : State() object Liquid : State() object Gas : State() } sealed class Event { object OnMelted : Event() object OnFroze : Event() object OnVaporized : Event() object OnCondensed : Event() }

Slide 220

Slide 220 text

State Machine DSL sealed class State { object Solid : State() object Liquid : State() object Gas : State() } sealed class Event { object OnMelted : Event() object OnFroze : Event() object OnVaporized : Event() object OnCondensed : Event() }

Slide 221

Slide 221 text

State Machine DSL sealed class State { object Solid : State() object Liquid : State() object Gas : State() } sealed class Event { object OnMelted : Event() object OnFroze : Event() object OnVaporized : Event() object OnCondensed : Event() }

Slide 222

Slide 222 text

State Machine DSL sealed class State { object Solid : State() object Liquid : State() object Gas : State() } sealed class Event { object OnMelted : Event() object OnFroze : Event() object OnVaporized : Event() object OnCondensed : Event() }

Slide 223

Slide 223 text

State Machine DSL sealed class State { object Solid : State() object Liquid : State() object Gas : State() } sealed class Event { object OnMelted : Event() object OnFroze : Event() object OnVaporized : Event() object OnCondensed : Event() }

Slide 224

Slide 224 text

State Machine DSL sealed class State { object Solid : State() object Liquid : State() object Gas : State() } sealed class Event { object OnMelted : Event() object OnFroze : Event() object OnVaporized : Event() object OnCondensed : Event() }

Slide 225

Slide 225 text

State Machine DSL sealed class State { object Solid : State() object Liquid : State() object Gas : State() } sealed class Event { object OnMelted : Event() object OnFroze : Event() object OnVaporized : Event() object OnCondensed : Event() }

Slide 226

Slide 226 text

State Machine DSL StateMachine.create { initialState(Solid) state { on { transitionTo(Liquid) } } state { on { transitionTo(Solid) } on { transitionTo(Gas) } } state { on { transitionTo(Liquid) } } }

Slide 227

Slide 227 text

State Machine DSL StateMachine.create { initialState(Solid) state { on { transitionTo(Liquid) } } state { on { transitionTo(Solid) } on { transitionTo(Gas) } } state { on { transitionTo(Liquid) } } }

Slide 228

Slide 228 text

State Machine DSL StateMachine.create { initialState(Solid) state { on { transitionTo(Liquid) } } state { on { transitionTo(Solid) } on { transitionTo(Gas) } } state { on { transitionTo(Liquid) } } }

Slide 229

Slide 229 text

State Machine DSL StateMachine.create { initialState(Solid) state { on { transitionTo(Liquid) } } state { on { transitionTo(Solid) } on { transitionTo(Gas) } } state { on { transitionTo(Liquid) } } }

Slide 230

Slide 230 text

State Machine DSL StateMachine.create { initialState(Solid) state { on { transitionTo(Liquid) } } state { on { transitionTo(Solid) } on { transitionTo(Gas) } } state { on { transitionTo(Liquid) } } }

Slide 231

Slide 231 text

State Machine DSL StateMachine.create { initialState(Solid) state { on { transitionTo(Liquid) } } state { on { transitionTo(Solid) } on { transitionTo(Gas) } } state { on { transitionTo(Liquid) } } }

Slide 232

Slide 232 text

State Machine DSL StateMachine.create { initialState(Solid) state { on { transitionTo(Liquid) } } state { on { transitionTo(Solid) } on { transitionTo(Gas) } } state { on { transitionTo(Liquid) } } }

Slide 233

Slide 233 text

State Machine DSL StateMachine.create { initialState(Solid) state { on { transitionTo(Liquid) } } state { on { transitionTo(Solid) } on { transitionTo(Gas) } } state { on { transitionTo(Liquid) } } }

Slide 234

Slide 234 text

State Machine DSL StateMachine.create { initialState(Solid) state { on { transitionTo(Liquid) } } state { on { transitionTo(Solid) } on { transitionTo(Gas) } } state { on { transitionTo(Liquid) } } }

Slide 235

Slide 235 text

State Machine DSL StateMachine.create { initialState(Solid) state { on { transitionTo(Liquid) } } state { on { transitionTo(Solid) } on { transitionTo(Gas) } } state { on { transitionTo(Liquid) } } }

Slide 236

Slide 236 text

State Machine DSL StateMachine.create { initialState(Solid) state { on { transitionTo(Liquid) } } state { on { transitionTo(Solid) } on { transitionTo(Gas) } } state { on { transitionTo(Liquid) } } }

Slide 237

Slide 237 text

State Machine DSL StateMachine.create { initialState(Solid) state { on { transitionTo(Liquid) } } state { on { transitionTo(Solid) } on { transitionTo(Gas) } } state { on { transitionTo(Liquid) } } }

Slide 238

Slide 238 text

State Machine DSL StateMachine.create { initialState(Solid) state { on { transitionTo(Liquid) } } state { on { transitionTo(Solid) } on { transitionTo(Gas) } } state { on { transitionTo(Liquid) } } }

Slide 239

Slide 239 text

State Machine DSL StateMachine.create { initialState(Solid) state { on { transitionTo(Liquid) } } state { on { transitionTo(Solid) } on { transitionTo(Gas) } } state { on { transitionTo(Liquid) } } }

Slide 240

Slide 240 text

State Machine DSL StateMachine.create { initialState(Solid) state { on { transitionTo(Liquid) } } state { on { transitionTo(Solid) } on { transitionTo(Gas) } } state { on { transitionTo(Liquid) } } }

Slide 241

Slide 241 text

State Machine DSL StateMachine.create { initialState(Solid) state { on { transitionTo(Liquid) } } state { on { transitionTo(Solid) } on { transitionTo(Gas) } } state { on { transitionTo(Liquid) } } }

Slide 242

Slide 242 text

State Machine DSL StateMachine.create { initialState(Solid) state { on { transitionTo(Liquid) } } state { on { transitionTo(Solid) } on { transitionTo(Gas) } } state { on { transitionTo(Liquid) } } }

Slide 243

Slide 243 text

State Machine DSL StateMachine.create { initialState(Solid) state { on { transitionTo(Liquid) } } state { on { transitionTo(Solid) } on { transitionTo(Gas) } } state { on { transitionTo(Liquid) } } }

Slide 244

Slide 244 text

State Machine DSL StateMachine.create { initialState(Solid) state { on { transitionTo(Liquid) } } state { on { transitionTo(Solid) } on { transitionTo(Gas) } } state { on { transitionTo(Liquid) } } }

Slide 245

Slide 245 text

State Machine DSL StateMachine.create { initialState(Solid) state { on { transitionTo(Liquid) } } state { on { transitionTo(Solid) } on { transitionTo(Gas) } } state { on { transitionTo(Liquid) } } }

Slide 246

Slide 246 text

Testing State Machine StateMachine.create { // ... }

Slide 247

Slide 247 text

Testing State Machine val stateMachine = StateMachine.create { // ... }

Slide 248

Slide 248 text

Testing State Machine val stateMachine = StateMachine.create { // ... } assertThat(stateMachine.state).isEqualTo(Solid) // When stateMachine.transition(OnMelted) // Then assertThat(stateMachine.state).isEqualTo(Liquid)

Slide 249

Slide 249 text

Testing State Machine val stateMachine = StateMachine.create { // ... } assertThat(stateMachine.state).isEqualTo(Solid) // When stateMachine.transition(OnMelted) // Then assertThat(stateMachine.state).isEqualTo(Liquid)

Slide 250

Slide 250 text

Testing State Machine val stateMachine = StateMachine.create { // ... } assertThat(stateMachine.state).isEqualTo(Solid) // When stateMachine.transition(OnMelted) // Then assertThat(stateMachine.state).isEqualTo(Liquid)

Slide 251

Slide 251 text

Testing State Machine val stateMachine = StateMachine.create { // ... } assertThat(stateMachine.state).isEqualTo(Solid) // When stateMachine.transition(OnMelted) // Then assertThat(stateMachine.state).isEqualTo(Liquid)

Slide 252

Slide 252 text

Testing State Machine val stateMachine = StateMachine.create { // ... } assertThat(stateMachine.state).isEqualTo(Solid) // When stateMachine.transition(OnMelted) // Then assertThat(stateMachine.state).isEqualTo(Liquid)

Slide 253

Slide 253 text

State Machine in Scarlet

Slide 254

Slide 254 text

State Machine in Scarlet

Slide 255

Slide 255 text

State Management Plugins

Slide 256

Slide 256 text

State Management Plugins

Slide 257

Slide 257 text

State Management Plugins

Slide 258

Slide 258 text

State Management Plugins

Slide 259

Slide 259 text

State Management Plugins

Slide 260

Slide 260 text

State Management Plugins

Slide 261

Slide 261 text

State Management Plugins • Lifecycle • Declare when to connect and to keep retrying • Backoff Strategy • Declare when to retry

Slide 262

Slide 262 text

State Management in Scarlet

Slide 263

Slide 263 text

Scarlet

Slide 264

Slide 264 text

Scarlet • Easy to get started

Slide 265

Slide 265 text

Scarlet • Easy to get started • Declarative API

Slide 266

Slide 266 text

Scarlet • Easy to get started • Declarative API • Built-in plugins

Slide 267

Slide 267 text

Scarlet • Easy to get started • Declarative API • Built-in plugins • Customizable

Slide 268

Slide 268 text

Scarlet • Easy to get started • Declarative API • Built-in plugins • Customizable • Easy to maintain

Slide 269

Slide 269 text

Scarlet • Easy to get started • Declarative API • Built-in plugins • Customizable • Easy to maintain • Reliable

Slide 270

Slide 270 text

Scarlet • Easy to get started • Declarative API • Built-in plugins • Customizable • Easy to maintain • Reliable • StateMachine DSL

Slide 271

Slide 271 text

Scarlet • Easy to get started • Declarative API • Built-in plugins • Customizable • Easy to maintain • Reliable • StateMachine DSL • State management plugins

Slide 272

Slide 272 text

Taming WebSocket with Scarlet github.com/Tinder/Scarlet github.com/Tinder/StateMachine github.com/zhxnlai

Slide 273

Slide 273 text

Lifecycle const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .backoffStrategy(BACKOFF_STRATEGY) .lifecycle(createAppForegroundAndUserLoggedInLifecycle()) .build() val gdaxService = scarlet.create()

Slide 274

Slide 274 text

Lifecycle const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .backoffStrategy(BACKOFF_STRATEGY) .lifecycle(AndroidLifecycle.ofApplicationForeground()) .build() val gdaxService = scarlet.create()

Slide 275

Slide 275 text

Lifecycle

Slide 276

Slide 276 text

Lifecycle • Started

Slide 277

Slide 277 text

Lifecycle • Started • Connect and keep retrying

Slide 278

Slide 278 text

Lifecycle • Started • Connect and keep retrying • Stopped

Slide 279

Slide 279 text

Lifecycle • Started • Connect and keep retrying • Stopped • Disconnect

Slide 280

Slide 280 text

Lifecycle

Slide 281

Slide 281 text

Lifecycle

Slide 282

Slide 282 text

Lifecycle

Slide 283

Slide 283 text

Lifecycle

Slide 284

Slide 284 text

Built-in Lifecycle AndroidLifecycle.ofApplicationForeground(application)

Slide 285

Slide 285 text

Write you own Lifecycle AndroidLifecycle.ofApplicationForeground(application) loggedInLifecycle

Slide 286

Slide 286 text

Combine Lifecycle AndroidLifecycle.ofApplicationForeground(application) .combineWith(loggedInLifecycle)

Slide 287

Slide 287 text

Combine Lifecycle fun createAppForegroundAndUserLoggedInLifecycle(): Lifecycle { return AndroidLifecycle.ofApplicationForeground(application) .combineWith(loggedInLifecycle) }

Slide 288

Slide 288 text

Combine Lifecycle fun createAppForegroundAndUserLoggedInLifecycle(): Lifecycle { return AndroidLifecycle.ofApplicationForeground(application) .combineWith(loggedInLifecycle) }

Slide 289

Slide 289 text

Lifecycle const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .backoffStrategy(BACKOFF_STRATEGY) .lifecycle(AndroidLifecycle.ofApplicationForeground()) .build() val gdaxService = scarlet.create()

Slide 290

Slide 290 text

Lifecycle const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .backoffStrategy(BACKOFF_STRATEGY) .lifecycle(createAppForegroundAndUserLoggedInLifecycle()) .build() val gdaxService = scarlet.create()

Slide 291

Slide 291 text

Lifecycle const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .backoffStrategy(BACKOFF_STRATEGY) .lifecycle(createAppForegroundAndUserLoggedInLifecycle()) .build() val gdaxService = scarlet.create()

Slide 292

Slide 292 text

Backoff Strategy const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .backoffStrategy(ExponentialWithJitterBackoffStrategy()) .lifecycle(createAppForegroundAndUserLoggedInLifecycle()) .build() val gdaxService = scarlet.create()

Slide 293

Slide 293 text

Backoff Strategy

Slide 294

Slide 294 text

Backoff Strategy

Slide 295

Slide 295 text

Backoff Strategy • Declare when to retry

Slide 296

Slide 296 text

Backoff Strategy const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .backoffStrategy(LinearBackoffStrategy()) .lifecycle(createAppForegroundAndUserLoggedInLifecycle()) .build() val gdaxService = scarlet.create()

Slide 297

Slide 297 text

Backoff Strategy const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .backoffStrategy(ExponentialBackoffStrategy()) .lifecycle(createAppForegroundAndUserLoggedInLifecycle()) .build() val gdaxService = scarlet.create()

Slide 298

Slide 298 text

Backoff Strategy const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .backoffStrategy(ExponentialWithJitterBackoffStrategy()) .lifecycle(createAppForegroundAndUserLoggedInLifecycle()) .build() val gdaxService = scarlet.create()

Slide 299

Slide 299 text

Backoff Strategy const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .backoffStrategy(CustomBackoffStrategy()) .lifecycle(createAppForegroundAndUserLoggedInLifecycle()) .build() val gdaxService = scarlet.create()

Slide 300

Slide 300 text

Backoff Strategy const val GDAX_URL = "wss://ws-feed.gdax.com" val scarlet = Scarlet.Builder() .webSocketFactory(okHttpClient.newWebSocketFactory(GDAX_URL)) .addMessageAdapterFactory(MoshiMessageAdapter.Factory()) .addStreamAdapterFactory(RxJava2StreamAdapter.Factory()) .backoffStrategy(CustomBackoffStrategy()) .lifecycle(createAppForegroundAndUserLoggedInLifecycle()) .build() val gdaxService = scarlet.create()