Date post: | 21-Jan-2017 |
Category: |
Technology |
Upload: | spring-io |
View: | 2,519 times |
Download: | 7 times |
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
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
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
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
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
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
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);
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
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
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
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
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); });
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
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
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
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
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
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
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);};
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
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
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
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
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(); }}
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
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
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
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
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
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
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.
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
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
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
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
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
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
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
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(); } }
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