Beginning with version 4.0 of the Spring Framework supports WebSocket, below I will detail the content Spring WebSocket library. Including Spring Framework is how to support WebSocket way messaging in Web applications, as well as how to use the STOMP protocol application layer protocol --WebSocket sub-protocol.
1, WebSocket protocol description
WebSocket protocol is an important function of RFC-6455 specification defines a Web areas: full-duplex, two-way communication that is between the client and the server. It is an exciting feature in the industry have explored this area for a long time, use of technology, including Java Applet, XMLHttpRequest, Adobe Flash, ActiveXObject, various Comet technology, the server sends events.
We need to understand that before using WebSocket protocol, you need to use the HTTP protocol used to build the initial handshake. It relies on a mechanism - the establishment of HTTP, request protocol upgrade (or called protocol conversion). When the server agree that it will respond to HTTP status code 101, it agreed handover protocol. Suppose success Handshake via TCP sockets, HTTP protocol Upgrade requests, the client and the server can send messages to each other.
Spring Framework 4.0+ introduces a new module, the spring-websocket module. It provides support for WebSocket communication. It is compatible with Java WebSocket API specification JSR-356, while providing additional functionality
2, WebSocket downgrade option
Browser support for WebSocket is not fast, IE browser is the 10th edition support only. In addition, some agents also limit WebSocket communication tools. Thus, even now to develop WebSocket application downgrade options is essential in order to support the scenario does not work with analog WebSocket API's. Spring Framework provides this transparent downgrade - use SockJS agreement. This program can be configured to automatically switch, without modifying the application code.
3, messaging infrastructure
In addition to using WebSocket challenges of development, there is a difficulty in that design considerations.
Currently REST architecture is a widely accepted, easy to understand, build infrastructure suitable for modern Web applications. REST architecture relies on a lot of URL, and several HTTP methods, the use of links, the principles remain stateless.
In contrast WebSocket applications may use only one URL for the initial HTTP handshake. All subsequent messages shared this TCP connection, the message in this connection on the two-way flow. This is seen, it is completely different from the REST architecture, asynchronous, event-driven, message-passing architecture. WebSocket architecture is quite similar to traditional messaging programs (such as JMS, AMQP).
Spring Framework 4.0 introduces a new module --spring-messaging module, which contains a lot from Spring Integration project abstract concepts, such as: Message message, the message channel MessageChannel, message handle MessageHandler like. This module also includes a set of notes, messages can be mapped onto a method with Spring MVC annotation-based programming model similar.
4, WebSocket support sub-protocol
WebSocket is a messaging architecture, it does not specify any particular messaging protocol. It is a thin layer on top of the TCP protocol, you can put the byte stream into the flow of messages (text goods binary). Followed by the application to interpret the message.
Different HTTP protocol, WebSocket protocol is an application-level protocol, it is very simple, and can not understand the incoming message, the message can not be routed or processed. Therefore, WebSocket protocol is the underlying application-level protocol, on the need for a framework to understand and process the message.
For this reason, WebSocket RFC defines the use of sub-protocol. During the handshake, the client and server can use the Header section Sec-WebSocket-Protocol to negotiate the use of sub-protocol - that the use of more advanced application-level protocol. Use sub-protocol is not essential, but even without the use of sub-protocols, applications still need to select a message format - allows the client and server mutually understandable format. This format can be customized, or specific frame, or use a standard messaging protocol.
Spring Framework provides support for the use of sub-STOMP protocol.
STOMP, Streaming Text Orientated Message Protocol, directional flow text message protocol. STOMP is a simple messaging protocol, is one for MOM (Message Oriented Middleware, message-oriented middleware) designed a simple text protocol.
STOMP provides a connection format interoperable, allowing the client to any STOMP STOMP message broker (Broker) interacts similar OpenWire protocol (A binary protocol).
5, under what scenario the use WebSocket
In Web applications, client and server side at a higher frequency and lower latency exchange event, suitable for use WebSocket. Therefore, WebSocket for finance, games, collaboration scenarios.
For other scenarios are not necessarily appropriate. For example, a newsletter subscription needs to display breaking news, the interval of a few minutes long polling is also possible, where the delay is acceptable.
Even in the low-latency requirements of application scenarios, if the number of message transmission is low (such as monitoring a network failure scenarios), you should consider using long polling technique.
And only in the high-frequency and low-latency messaging scenarios, the choice of WebSocket protocol is very appropriate. Even with such scenarios, there is still a choice WebSocket communication it? Or choose REST HTTP communication it?
The answer is based on the needs of the application may be. However, it is possible to use both techniques, the need for frequent exchange of data into the WebSocket achieve, and the REST API as a process of realization of business technology. In addition, when the REST API calls need to broadcast information to a plurality of clients it is to be achieved through a WebSocket connection.
Spring framework provides @Controller @RestController comments and notes, both of which can be used to handle processing and WebSocket message HTTP request. In addition, Spring MVC request processing method, processing method or request other applications, you can easily use WebSocket protocol to broadcast messages to all interested customers or end users specified.