from zero to hero with spring websocket

40
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ SPRINGONE2GX WASHINGTON, DC From 0 to Hero with Spring WebSocket Sergi Almar @sergialmar

Upload: spring-io

Post on 21-Jan-2017

2.518 views

Category:

Technology


7 download

TRANSCRIPT

Page 1: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

SPRINGONE2GXWASHINGTON, DC

From 0 to Hero with Spring WebSocket

Sergi Almar @sergialmar

Page 2: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Agenda

• Realtime Web • Evolution of the web • Server Sent Events

• WebSocket • Intro • Spring WebSocket • Scaling • Security

2

Page 3: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Agenda

• Realtime Web • Evolution of the web • Server Sent Events

• WebSocket • Intro • Spring WebSocket • Scaling • Security

3

Page 4: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

HOW MANY WEB APPLICATIONS WITH REALTIME NOTIFICATIONS DO YOU USE?

4

Page 5: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Evolution

5

HTTP 1.1

1999 2005

AJAX

2011

WebSocket

2015

HTTP/2

Page 6: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Polling

6

request

responserequest

responserequest

response

event

Page 7: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Polling

7

setInterval(function(){ $.ajax({ url: "server", success: function(data){

//Process }, dataType: “json"}); }, 30000);

Page 8: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Long Polling

8

request

eventresponserequest

response event

Page 9: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

HTTP Streaming

9

request

event

response

event

event

Page 10: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Agenda

• Realtime Web • Evolution of the web • Server Sent Events

• WebSocket • Intro • Spring WebSocket • Scaling • Security

10

Page 11: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Server Sent Events

• uni-directional • server push

• built on top of HTTP • a form of HTTP streaming

• long-loved HTTP connection

• EventSource API

11

Page 12: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

EventSource API

12

var source = new EventSource("/metrics"); source.addEventListener('memory', function(event) { console.log(event.data); });

source.addEventListener('uptime', function(event) { console.log(event.data); });

Page 13: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Spring SSE

13

@RequestMapping("/metrics") public SseEmitter subscribeMetrics() { SseEmitter emitter = new SseEmitter(); // Save emitter for further usage return emitter; }

• return SseEmitter from method handler (since Spring 4.2)

• use onCompletion() to be notified when async request completes • also called when network errors occur

Page 14: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

DEMO

14

Page 15: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Agenda

• Realtime Web • Evolution of the web • Server Sent Events

• WebSocket • Intro • Spring WebSocket • Scaling • Security

15

Page 16: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

WebSocket Protocol

• Real-time full duplex communication over TCP • Standardized by the IETF (RFC 6455)

• Uses port 80 / 443 (URL scheme ws:// wss://)

• Small overhead for text messages (frames) • 0x00 for frame start, 0xFF for frame end (vs HTTP 1Kb)

• Use cases: games, collaborative apps, financial tickets, social feeds, chat...

16

Page 17: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Realtime WebSocket-based APIs

17

Page 18: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

WebSocket - Anyone interested?

18

Page 19: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

WebSocket API

19

var ws = new WebSocket("ws://localhost/ws");

ws.onopen = function () { ws.send('Here I am!');};

ws.onmessage = function (event) { console.log('message: ' + event.data);};

ws.onclose = function (event) { console.log('closed:' + event.code);};

Page 20: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Subprotocols

• WebSocket doesn't define any application protocol • As opposed to HTTP

• Too low level, applications need to interpret meaning of messages

• A subprotocol can be negotiated during handshake • STOMP, WAMP, MQTT, XMPP...

• Spring WebSocket supports STOMP

20

STOMP

WebSocket

TCP

Page 21: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

WebSocket clients

• Not only browsers…

21

• Mobile clients

• Server to server communication • SockJS and STOMP clients

available in Spring

Page 22: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Agenda

• Realtime Web • Evolution of the web • Server Sent Events

• WebSocket • Intro • Spring WebSocket • Scaling • Security

22

Page 23: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Spring WebSocket - Anyone interested?

23

Page 24: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

WebSocket Config

24

@Configuration@EnableWebSocketpublic class WebSocketConfig implements WebSocketConfigurer {

@Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry.addHandler(echoHandler(), "/echo"); }

@Bean public EchoHandler echoHandler() { return new EchoHandler(); }}

Page 25: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

CODING TIME

25

Page 26: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

STOMP over WebSocket

• When using STOMP, we will have an event-driven, message architecture

• Similar to JMS or AMQP

• Types of destinations

• Application destinations

• Messages routed to controller message handler methods

• Broker destinations

• Messages routed to the message broker

• User destinations

• Messages routed to a specific user

26

Page 27: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Broker

• Two options:

• SimpleBroker

• built-in broker

• everything in memory

• BrokerRelay

• Forwards messages to a STOMP broker (RabbitMQ, ActiveMQ…)

• Better for scaling

27

Page 28: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

CODING TIME

28

Page 29: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Agenda

• Realtime Web • Evolution of the web • Server Sent Events

• WebSocket • Intro • Spring WebSocket • Scaling • Security

29

Page 30: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Time to Scale

30

cf scale spring-questions -i 2

Page 31: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Messaging users

31

Bob Alice Joe

Bob and Alice are connected to instance1

Joe is connected to instance2

Bob sends a message to Alice (/user/alice/queue/messages).As they are on the same instance, the user destination can be resolved

Alice sends a message to Joe.Instance1 cannot resolve the user destination and message gets lost.

Page 32: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Resolving user destinations

32

@Override public void configureMessageBroker(MessageBrokerRegistry registry) { registry.setApplicationDestinationPrefixes("/app") .enableStompBrokerRelay("/queue", "/topic")

.setUserDestinationBroadcast("/topic/unresolved-user-dest"); }

• When a user destination cannot be resolved, a message will be broadcasted • This allows other instances to resolve the destination

Page 33: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Connected users

33

Bob Alice Joe

Bob and Alice are connected to instance1

Joe is connected to instance2

How many users connected?

2

How many users connected?

1

Page 34: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

SimpUserRegistry

34

• A registry for the currently connected users and subscriptions

• Two implementations: • DefaultSimpUserRegistry (default strategy)

• stores everything in memory • MultiServerUserRegistry:

• shares user registries across multiple servers

Page 35: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

UserRegistry Sync

35

Bob Alice Joe

Bob and Alice are connected to instance1

Joe is connected to instance2

How many users connected?

3

How many users connected?

3sync sync

Page 36: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Resolving user destinations

36

@Override public void configureMessageBroker(MessageBrokerRegistry registry) { registry.setApplicationDestinationPrefixes("/app") .enableStompBrokerRelay("/queue", "/topic")

.setUserDestinationBroadcast("/topic/unresolved-user-dest"); }

• When a user destination cannot be resolved, a message will be broadcasted • This allows other instances to resolve the destination

Page 37: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Agenda

• Realtime Web • Evolution of the web • Server Sent Events

• WebSocket • Intro • Spring WebSocket • Scaling • Security

37

Page 38: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

WebSocket Security

38

• Handshake request is a simple HTTP request • Protect it as a normal URL

• Use WebSocket Secure connection (wss://)

• Origin

Page 39: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Message Security

39

@Configuration public class WebSocketSecurityConfig extends AbstractSecurityWebSocketMessageBrokerConfigurer {

@Override protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) { messages // restrict subscription with role ADMIN .simpSubscribeDestMatchers("/topic/admin.notifications").hasRole("ADMIN") // users cannot send to these broker destinations, only the application can .simpMessageDestMatchers("/topic/orders", "/topic/order.conf").denyAll() .anyMessage().authenticated(); } }

Page 40: From Zero to Hero with Spring WebSocket

Unless otherwise indicated, these s l ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/ 40

Q & A Thanks!

See you at Spring I/O 2016 Barcelona, May 19-20

Learn more http://www.infoq.com/presentations/spring-4-websockets

@sergialmar salmar

www.springio.net