Implementing a Spring Websocket Server and Client

This article guides you through the implementation of a WebSocket server and client based on the Spring Framework. It demonstrates full duplex communication and how the server can push messages to the client. You will learn a little about how WebSockets work, as well as the STOMP messaging format used for communication between the server and the client.

To get the most out of his article, you need good knowledge of Java, along with some exposure to the Spring Framework, especially Spring MVC. The source code for the server that we are oing to implement is available on GitHub.

Cliff’s Notes On Websockets, STOMP, and Spring

To get you up and running with Websocket on the JVM, here’s a quick intro to what we’ll need later in the article.

Introduction to WebSockets

WebSocket is a full-duplex communications protocol layered over TCP. It is typically used for interactive communication between a user’s browser and a back-end server. An example would be a chat server with real-time communications between the server and the connected clients. Another example would be a Stock Trading application where the server sends stock price variations to subscribed clients without an explicit client request.

A WebSocket is a communication channel which uses TCP as the underlying protocol. It is initiated by the client sending a HTTP request to the server requesting a connection upgrade to WebSocket. If the server supports WebSockets, the client request is granted and a WebSocket connection is established between the two parties. After this connection is established, all communication happens over the WebSocket and the HTTP protocol is no longer used.

This article does a very good job of explaining the details of how WebSockets work.

STOMP over WebSocket

The WebSocket communication protocol itself does not mandate any particular messaging format. It is up to the applications to agree upon the format of the messages exchanged. This format is referred to as the subprotocol. (Kinda similar to how the web browser and web server have agreed to using the HTTP protocol over TCP sockets.)

One commonly used format is the STOMP protocol (Streaming Text Oriented Message Protocol) used for general purpose messaging. Various message oriented middle-ware (MOM) systems such as Apache ActiveMQ, HornetQ and RabbitMQ support STOMP.

The Spring Framework implements WebSocket communication with STOMP as the messaging protocol.

Spring Support for WebSockets

Spring Framework provides support for WebSocket messaging in web applications. Spring version 4 includes a new module spring-websocket, which is used to add WebSocket support to the server and the client.

The Spring Framework Guide includes a detailed HOWTO demonstrating a sample server implementation. However, it is not necessary to have read that guide; we cover all the details required for understanding the implementation in this article.

One shortcoming of the Spring Framework Guide is that no information is presented on implementing a Java/Spring Client to communicate with the server. We address that issue in this article. As you will see, the implementation requires correct incantations to get it working just right.

Spring Chat Server Implementation

With the prerequisites out of the way, it is time to implement the chat server. Here’s a rundown of how it works:

  • The Spring chat server creates an HTTP endpoint (/chat), which is used for establishing the WebSocket channel.
  • Once the channel is established, the server and client exchange messages over the channel.
  • While the server listens to and responds to client messages, the client can also register a callback for “push” messages from the server. This allows the server to send notifications to the client as and when required without an explicit client request.

The message “push” from the server to the client is what makes the WebSocket communication different from normal HTTP interaction.

Continue reading %Implementing a Spring Websocket Server and Client%

You may also like...