Spring + WebSocket integration
SEMENOV OLEKSANDR
Agenda
1) The problem of synchronous data exchange model
2) Existing solutions
3) WebSocket
4) Subprotocols: STOMP
5) Spring MVC + WebSocket architecture
6) Drawbacks
7) Showcase
The problem
HTTP is synchronous, but sometimes asynchronous data receiving from
server is required
The problem
Existing solutions
Several ways to solve it:
1) Polling
2) Long polling
3) Endless iframe
4) HTML5: Server Side Events
5) HTML5: WebSocket
Polling
Long polling
Polling/long polling. Drawbacks
Self-initiated DDoS attack!
Polling/long polling. Drawbacks
1) Network overhead
a) SSL handshake can be initiated each time, and it is expensive
2) User presence tracking is complicated
3) In case of load balancer, each time user will be connected to
different server
Server Side Events
Server Side Events. API
Server Side Events. Drawbacks
Browser support is limited
Any alternatives?
WebSocket
WebSocket features
1) Bidirectional
2) Binary
3) No data overhead
4) Persistent connection
5) Supported by most proxies and load-balancers
WebSocket handshake
WebSocket handshake
WebSocket advantages
WebSocket vs REST approach
REST
1) Each URL mapped to resource
2) Each HTTP method has predefined semantics
WebSocket
1) Single URL for handshake
2) No predefined semantics
What does it mean?
Communications via WebSocket is much closer
to messaging approach*
But no concrete messaging protocols are defined
WebSocket subprotocols
1) WAMP – provides RPC + messaging features.
2) STOMP – provides messaging features only. Can be easily integrated
with message brokers like ActiveMQ, RabbitMQ etc.
3) 20+ other subprotocols
STOMP
Integration with Spring MVC
Client side
1) stomp.js library
Server side
1) Spring 4+
Client-side configuration
Server-side configuration
Server-side configuration
Other features
1) Exception handling
2) Flexible destinations
3) External message broker pluging-in
Internal architecture
External broker case
Drawbacks
What if you want connect directly to external message
broker and put message there, not via WebSocket endpoint?
It is impossible with current Spring architecture. Workarounds required.
Conclusions
1) WebSocket is standardized technology for bidirectional data
exchange in web.
2) WebSocket is fast and low latency
3) Spring 4+ easily integrates with websockets using STOMP
subprotocol.
4) Spring 4+ internal architecture is not ideal
Thank you!

Spring + WebSocket integration