You can find many examples available on the web for spring websocket integration but most of them are using some messaging protocol like STOMP. Though Spring provides STOMP support out of the box but use of any messaging protocol to connect to websocket is not mandatory. You can use raw websocket by defining your own messaging protocol to connect to spring websocket using TextWebSocketHandler. In this post we will be creating a websocket connection using Spring but without STOMP and SockJs.
If you are looking for spring websocket example with STOMP and Spring Security, then visit here spring websocket example with STOMP.
STOMP stands for Streaming Text Oriented Messaging Protocol. As per wiki, STOMP is a simple text-based protocol, designed for working with message-oriented middleware (MOM). It provides an interoperable wire format that allows STOMP clients to talk with any message broker supporting the protocol.
This means when we do not have STOMP as a client, the message sent lacks of information to make Spring route it to a specific message handler method. So, if we could create a mechanism that can make Spring to route to a specific message handler then probably we can make websocket connection without STOMP.
We concluded that if we do not want to use STOMP, then we have to define our custom message protocol handler that should handle all the messages coming from any websocket client. Before defining any custom message handler class, we need to identify what format of message our handler class expect from the client. It can be either XML or Json or of any other format.
In this example we assume that the websocket client will be communicating with the server in JSON. Hence our handler will be a JSON message handler and we can define this in Spring by extending TextWebSocketHandler.java
TextWebSocketHandler is a class in Spring library that extends AbstractWebSockethandler class and primarily meant for handling only text messages. So, if you are expecting any binary messages from your websocket client then defining your custom handler class by extending TextWebSocketHandler is of no use as binary messages will be rejected with CloseStatus.NOT_ACCEPTABLE.
While coming to the code implementation, we will be creating a simple web app using Spring websocket where message transfer between the client and server will happen via websocket without STOMP. Let us start by defining our project structure.
Let us start by defining
@SpringBootApplication: This is a convenience annotation that is equivalent to declaring
Now let us define our custom handler class by extending
TextWebSocketHandler.java that will override the protocol to handle the text message. Our handler will be capable of handling Json message. This class records all the session once any websocket connection is established and broadcasts the message to all the sessions once any message is received.
Instead of broadcasting message to all the clients, you can also configure to send message to a particular session. This configuration is also explained in coming section.SocketHandler.class
Now let us define our websocket configurations. The below configuration will register websocket handler at
/name and define our custom handler class that will handle all the messages from the websocket client.
This is it for server side. Now let's jump to client side code.
Here we have defined 3 methods each for creating websocket connection when user clicks on connect button, sending message to the server once send button is clicked and another for listening the server response that will come from the server.
1. Run Application.java as a java application and the main method will execute.
2. In the browser make request at http:localhost:8080.
3. Create websocket connection by clicking connect button and enter the name and click on send.
4. The handler class at server side will handle the message and return the greeting message as a response which will be shown in the browser as below:
Above implementation will broadcast message to all the sessions and hence all the client will receive the messaage at a time. To send message to a single client we need to tweak our
Sockethandler.java as below.
This change in SocketHandler.java will ensure that the message is sent to a single session.
I hope this article served you that you were looking for. If you have anything that you want to add or share then please share it below in the comment section.
4. Spring Boot Mvc Example
Contact us: firstname.lastname@example.org