WebSocket Application With Spring Boot Step By Step – Source Code On GitHub

WebSocket is used to develop two-way communication between server and browser. For example, in a chat application, if a user sends a message to a different user, the application sends that message to the server first and the server informs the application of the second user . If we don’t use WebSocket, the application or the client will have to pull the server data continuously for any new updates.

WebSocket Application With Spring Boot:

In Sprint Boot, we can easily create a WebSocket application. In this tutorial, I will show you how to create a WebSocket server in Spring boot. We will learn how to connect and disconnect from a socket server and how to send a simple text message. The client will subscribe to the server, it will accept a string and push a separate string to the subscriber client.

Configuration :

In this example, we are using IntelliJ-IDEA IDE with Gradle build system to create the application. You can follow the same process to create it on a maven build system.

Create one Basic app :

Let’s create one basic Spring boot application. Create one basic Spring boot application using start.spring. In this example, we are using Gradle but you can also use Maven as the build system. Open the application on your favorite editor before moving to the next step.

We will use WebSocket in this application.

Adding dependencies :

Open your project level build.gradle file and add the following dependencies :

Note that you can use the same dependencies for a maven project as well and the version may be different than the above we are using at the time of writing this article.

Let’s build the App :

We need to create four components in the application.

  1. Model class to hold the type of data
  2. A controller class to define the routes
  3. Configuration class to configure spring to enable WebSocket and STOMP messaging
  4. Browser client or an HTML file and a javascript file for user interaction.

We will create each of these components one by one :

1. Model class :

We need two model classes. The controller will get the data as a Java object, modify it and returns one new Java object. This new object will be shown to to the user.

The service gets STOMP messages whose body is a JSON. It uses the model class to model this object. Create one file InputMessage.java inside your project :

This is the input message class. Once the service will receive that message, it will process it and send back a different model with a different class :

The return value is in JSON format. Spring will use the Jackson JSON library for this conversion. Both of these classes are POJO.

2. Controller class :

Second part is to create the message handling controller class. Create one class called ‘MessageController.java’ with the below code :

This class has one method defined called ‘sendMessage’. It takes one ‘InputMessage’ object and returns one ‘OutputMessage’ object.

3. Configuration class :

The configuration class should implement the ‘WebSocketMessageBrokerConfigurer’ class. Create one file Config.java with the below code :

Make sure that your SpringBootApplication class is configured like below :

That’s it. With these five files, our socket application is ready. The only thing left is the front end part of the HTML, CSS changes.

Browser client :

We need one HTML and one java-script file to create the basic app. Both files should be placed in the ‘resources/static’ folder. Create one ‘index.html’ file with the below code :

Inside the same folder, create one app.js file :

Your application is ready at this point. Start it and open ‘localhost:8080’ on a server. It will show a screen like below :WebSocket Application With Spring Boot

Click on the start button. It will change the message as ‘connected’ and one input field will be shown with a ‘send’ button.

Enter anything in the input field and hit the ‘send’ button, the receiver will receive that message via socket and print it out at the bottom :

WebSocket Vs REST :

WebSocket and REST are two different concepts. Following are the main differences between REST and WebSocket :

  1. WebSocket is a stateful protocol but REST is a stateless protocol.
  2. REST applications are normally used for occasional communication but WebSockets are ideal for continuous communication
  3. With REST applications, a new TCP connection is used for each request but WebSocket uses the same TCP connection over the life of the socket connection.
  4. Communication cost is lower for WebSocket as compared to REST APIs
  5. In WebSocket, it is possible for both server and client to send and receive data at the same time

WebSocket has few advantages than RESTful APIs but it is not a direct replacement for REST. If multiple clients need to be synced at the same time, ‘WebSocket’ is the best option. But, if the clients are only reading and writing data to the server independent of each other, REST will do the work.

3 Comments
  1. November 19, 2021 | Reply
  2. sitrap
    January 21, 2021 | Reply
    • Shilpa
      January 28, 2021 | Reply

Add a Comment

Your email address will not be published. Required fields are marked *