From Zero to Hero with Spring WebSocket

43 %
57 %
Information about From Zero to Hero with Spring WebSocket

Published on September 23, 2015

Author: SpringCentral

Source: slideshare.net

1. 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

2. 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/ Agenda • Realtime Web • Evolution of the web • Server Sent Events • WebSocket • Intro • Spring WebSocket • Scaling • Security 2

3. 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/ Agenda • Realtime Web • Evolution of the web • Server Sent Events • WebSocket • Intro • Spring WebSocket • Scaling • Security 3

4. 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/ HOW MANY WEB APPLICATIONS WITH REALTIME NOTIFICATIONS DO YOU USE? 4

5. 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/ Evolution 5 HTTP 1.1 1999 2005 AJAX 2011 WebSocket 2015 HTTP/2

6. 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/ Polling 6 request response request response request response event

7. 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/ Polling 7 setInterval(function(){ $.ajax({ url: "server", success: function(data){ //Process }, dataType: “json"}); }, 30000);

8. 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/ Long Polling 8 request event response request response event

9. 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/ HTTP Streaming 9 request event response event event

10. 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/ Agenda • Realtime Web • Evolution of the web • Server Sent Events • WebSocket • Intro • Spring WebSocket • Scaling • Security 10

11. 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/ Server Sent Events • uni-directional • server push • built on top of HTTP • a form of HTTP streaming • long-loved HTTP connection • EventSource API 11

12. 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/ 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); });

13. 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/ 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

14. 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/ DEMO 14

15. 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/ Agenda • Realtime Web • Evolution of the web • Server Sent Events • WebSocket • Intro • Spring WebSocket • Scaling • Security 15

16. 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/ 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

17. 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/ Realtime WebSocket-based APIs 17

18. 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/ WebSocket - Anyone interested? 18

19. 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/ 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); };

20. 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/ 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

21. 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/ WebSocket clients • Not only browsers… 21 • Mobile clients • Server to server communication • SockJS and STOMP clients available in Spring

22. 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/ Agenda • Realtime Web • Evolution of the web • Server Sent Events • WebSocket • Intro • Spring WebSocket • Scaling • Security 22

23. 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/ Spring WebSocket - Anyone interested? 23

24. 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/ WebSocket Config 24 @Configuration @EnableWebSocket public class WebSocketConfig implements WebSocketConfigurer { @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry.addHandler(echoHandler(), "/echo"); } @Bean public EchoHandler echoHandler() { return new EchoHandler(); } }

25. 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/ CODING TIME 25

26. 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/ 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

27. 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/ Broker • Two options: • SimpleBroker • built-in broker • everything in memory • BrokerRelay • Forwards messages to a STOMP broker (RabbitMQ, ActiveMQ…) • Better for scaling 27

28. 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/ CODING TIME 28

29. 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/ Agenda • Realtime Web • Evolution of the web • Server Sent Events • WebSocket • Intro • Spring WebSocket • Scaling • Security 29

30. 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/ Time to Scale 30 cf scale spring-questions -i 2

31. 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/ 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.

32. 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/ 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

33. 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/ 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

34. 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/ 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

35. 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/ 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? 3 sync sync

36. 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/ 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

37. 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/ Agenda • Realtime Web • Evolution of the web • Server Sent Events • WebSocket • Intro • Spring WebSocket • Scaling • Security 37

38. 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/ WebSocket Security 38 • Handshake request is a simple HTTP request • Protect it as a normal URL • Use WebSocket Secure connection (wss://) • Origin

39. 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/ 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(); } }

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/ 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

Add a comment

Related pages

Schedule: From Zero to Hero with Spring WebSocket ...

CTO @ Voz.io, SpringSource Certified Instructor, Spring I/O organizer, javaHispano Core Member
Read more

Intro To WebSocket Applications with Spring Framework 4.0

z, ? toggle help (this) space, → next slide: shift-space, ← previous slide: d: toggle debug mode ## go to slide # c, t: table of contents (vi) f ...
Read more

Spring Framework 4.0 M1: WebSocket Support

The Spring WebSocket API targets framework use primarily. Applications can use it of course but we believe programming to a socket is too low level for ...
Read more

spring - spring4.0 websocket issue - Stack Overflow

... (2000); //simpMessagingTemplate.convertAndSend("/topic/test2","Send From backend"); return "This is message 2"; } Read more

Intro To WebSocket Applications with Spring Framework 4.0 ...

Intro To WebSocket Applications with Spring Framework 4.0 ... Learn more about WebSocket in Spring Framework at: ... Zero Effort Spring ...
Read more

WebSocketSession (Spring Framework 4.0.5.RELEASE API)

Return the map with attributes associated with the WebSocket session. When the WebSocketSession is created, ... Spring Framework. Prev Class; Next Class ...
Read more

Spring Websockets 4.1.6 with Tomcat 7.0.54 and Apache 2.4 ...

Spring Websockets 4.1.6 with Tomcat 7.0.54 and Apache 2.4. up vote 2 down vote favorite. I'm having some troubles running a simple application using Spring ...
Read more

Spring Framework 4.0 M2: WebSocket Messaging Architectures ...

... a WebSocket API is only the starting point for WebSocket-style ... The first milestone of Spring Framework 4.0 provided server-side support for ...
Read more

JHipster 2.0 Released with AngularJS improvements ...

... the Yeoman generator for Spring Boot/Angular projects, ... Migrated from Atmosphere to Spring Websockets. Added a Spark reporter for Metrics.
Read more

Spring 4.0.0.M3 WebSocket - Spring Forum

import javax.websocket.OnMessage; import. Docs; Guides; Projects; Blog; Questions; x. Home; Forum; ... Spring 4.0.0.M3 WebSocket Page Title Module. Move ...
Read more