websockets and java
DESCRIPTION
A short presentation on WebSockets and their Java server-side usage.TRANSCRIPT
![Page 1: WebSockets and Java](https://reader036.vdocuments.net/reader036/viewer/2022082920/554c0d31b4c9058e098b58ad/html5/thumbnails/1.jpg)
WebSocket
Bozhidar Bozhanov
...one day at home I decided I'm bored => a WebSocket-based game for http://computoser.com
![Page 2: WebSockets and Java](https://reader036.vdocuments.net/reader036/viewer/2022082920/554c0d31b4c9058e098b58ad/html5/thumbnails/2.jpg)
Usecases
● Games● Stock-exchange application● ...anything, really
![Page 3: WebSockets and Java](https://reader036.vdocuments.net/reader036/viewer/2022082920/554c0d31b4c9058e098b58ad/html5/thumbnails/3.jpg)
Why WebSocket?
● Push● Real-time communication● Resource-light● Intuitive, non-hacky
![Page 4: WebSockets and Java](https://reader036.vdocuments.net/reader036/viewer/2022082920/554c0d31b4c9058e098b58ad/html5/thumbnails/4.jpg)
Up to now
● Ajax polling (a.k.a. "are we there yet?")● Long-polling, streaming● Plugins – flash/applets/ActiveX● Problems: hacky, resource-heavy, server-
thread-unfriendly
![Page 5: WebSockets and Java](https://reader036.vdocuments.net/reader036/viewer/2022082920/554c0d31b4c9058e098b58ad/html5/thumbnails/5.jpg)
WebSocket
● Standard (HTML5) ● TCP, full-duplex● More effective on the server-side● High-level protocols ontop – STOMP, WAMP,
etc.
![Page 6: WebSockets and Java](https://reader036.vdocuments.net/reader036/viewer/2022082920/554c0d31b4c9058e098b58ad/html5/thumbnails/6.jpg)
Client-side
● Standard JavaScript API● Browser support – problematic● SockJS, socket.io, fallbacks
![Page 7: WebSockets and Java](https://reader036.vdocuments.net/reader036/viewer/2022082920/554c0d31b4c9058e098b58ad/html5/thumbnails/7.jpg)
Server-side
● node.js● Java – Java EE WebSocket API (JSR-356)● Servlet container implementations – thread
pools● Spring-MVC API, Spring Messaging API
![Page 8: WebSockets and Java](https://reader036.vdocuments.net/reader036/viewer/2022082920/554c0d31b4c9058e098b58ad/html5/thumbnails/8.jpg)
Java WebSocket API vs Spring
● Very similar● One handler per endpoint, differentiate based
on payload● Out-of-the-box dependency injection with spring● No annotation configuration in spring (yet)
![Page 9: WebSockets and Java](https://reader036.vdocuments.net/reader036/viewer/2022082920/554c0d31b4c9058e098b58ad/html5/thumbnails/9.jpg)
Messaging
● Alternative architecture● Using a message broker
(simple/ActiveMQ/RabbitMQ/..) behind spring● STOMP over WebSocket● Client subscribes for events
![Page 10: WebSockets and Java](https://reader036.vdocuments.net/reader036/viewer/2022082920/554c0d31b4c9058e098b58ad/html5/thumbnails/10.jpg)
DDD and TDD
● DDD comes naturally● Objects and their workflows are easily testable● Very few mocks, comparable to spring-mvc
@RequestMappings
![Page 11: WebSockets and Java](https://reader036.vdocuments.net/reader036/viewer/2022082920/554c0d31b4c9058e098b58ad/html5/thumbnails/11.jpg)
Security and authentication
● WSS vs WS (ws://example.com/game)● No same-origin policy. Origin header.● Validate input and output● Authentication – cookies? Cookie-like workflow.
![Page 12: WebSockets and Java](https://reader036.vdocuments.net/reader036/viewer/2022082920/554c0d31b4c9058e098b58ad/html5/thumbnails/12.jpg)
Conclusion
● It's cool :)