Spring + WebSocket integration

Post on 21-Jan-2017

150 views 1 download

transcript

Spring + WebSocket integrationSEMENOV OLEKSANDR

Agenda1) The problem of synchronous data exchange model2) Existing solutions3) WebSocket4) Subprotocols: STOMP5) Spring MVC + WebSocket architecture6) Drawbacks7) Showcase

The problem

HTTP is synchronous, but sometimes asynchronous data receiving from server is required

The problem

Existing solutionsSeveral ways to solve it:

1) Polling2) Long polling3) Endless iframe4) HTML5: Server Side Events5) HTML5: WebSocket

Polling

Long polling

Polling/long polling. Drawbacks

Self-initiated DDoS attack!

Polling/long polling. Drawbacks

1) Network overheada) 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 features1) 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

REST1) Each URL mapped to resource

2) Each HTTP method has predefined semantics

WebSocket3) Single URL for handshake

4) No predefined semantics

What does it mean?

Communications via WebSocket is much closer to messaging approach*

But no concrete messaging protocols are defined

WebSocket subprotocols1) 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.

Conclusions1) 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!