How to implement web Sockets in a spring boot application.
Posted By : Gursahib Singh | 30-Nov-2018
Summary: In this blog,the implementation of web sockets in a spring boot application is explained.The steps to do the same includes, including the required dependencies, creating a Message Handling Controller followed by its configuration in the application.
Prerequisite:Knowledge of Java and Spring-boot framework.
What are Web Sockets: Websockets consists of protocal to create a fast two way channel connection between a web browser and a server.It is used to overcome limitiations of HTTP for allowing low latency communication between user and the web service.
Steps to implement web sockets in a spring boot application are:
1. The first step is to include the following dependencies in the pom.xml file.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>webjars-locator</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>sockjs-client</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>stomp-websocket</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7</version> </dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.1.0</version>
</dependency>
2. The next step is to create Message Handling Controller for STOMP messaging. @Controller class can be used for routing the STOMP messages.
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;
@Controller
public class GreetingController {
@MessageMapping("/hello")
@SendTo("/topic/greetings")
public Greeting greeting(HelloMessage message) throws Exception {
Thread.sleep(1000); // simulated delay
return new Greeting("Hello, " + message.getName() + "!");
}
}
3. Now, the STOMP messaging is configured in the spring.For that, we need a WebSocketConfig class extending AbstractWebSocketMessageBrokerConfigurer class
as shown.
as shown.
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/tutorialspoint-websocket").withSockJS();
}
}
The @EnableWebSocketMessageBroker annotaion configures the web socket message broker for STOMP endpoints.
4. The code for the spring boot main application file is:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class WebsocketappApplication {
public static void main(String[] args) {
SpringApplication.run(WebsocketappApplication.class, args);
}
}
Conclusion:
This is how web socket can be implemented in a spring boot application.The web sockets help in achieving the interaction between the web client and web server with lower over heads and facilitates real time data transfer.Request for Proposal
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Gursahib Singh
Gursahib is a software developer having key skills in J2SE and J2EE. His hobbies are playing chess, reading and learning new softwares.