Chapter 18. Messaging with WebSocket and STOMP
This chapter covers
- Sending messages between the browser and the server
- Handling messages in Spring MVC controllers
- Sending user-targeted messages
In the previous chapter, you saw ways to send messages between applications using JMS and AMQP. Asynchronous messaging is a common form of communication between applications. But when one of those applications is running in a web browser, something a little different is needed.
WebSocket is a protocol providing full-duplex communication across a single socket. It enables, among other things, asynchronous messaging between a web browser and a server. Being full-duplex means that the server can send messages to the browser as well as the browser sending messages to the server.
Spring 4.0 introduced support for WebSocket communication, including
- A low-level API for sending and receiving messages
- A higher-level API for handling messages in Spring MVC controllers
- A messaging template for sending messages
- SockJS support to cope with the lack of WebSocket support in browsers, servers, and proxies
In this chapter, you’ll learn how to achieve asynchronous communication between a server and a browser-based application using Spring’s WebSocket features. We’ll start by looking at how to work with Spring’s low-level WebSocket API.