WebSocket – Create Spring WebSocket Application with SpringBoot + SockJS + STOMP
The WebSocket protocol provides new capability for web applications: full-duplex, two-way communication. So in the system where the client and server need to exchange data at high frequency and with low latency, WebSocket is the best solution. That is the reason for us to create a Spring WebSocket Application with JavaSampleApproach.
Related Post: Spring Boot WebSocket with Angular 5 Client | SockJS + STOMP
I. Spring WebSocket Application
1. Flow of messages
We create a Spring WebSocket Application with the below flow of messages:
Detail explanations:
- WebSocket clients connect to the WebSocket endpoint at '/jsa-stomp-endpoint'
- Subscriptions to '/topic/hi' pass through the response channel, then are forwarded to the In-memory broker (Simple Broker).
- User objects sent to '/jsa/hello' pass through the request channel then are forwarded to the spring WebController. WebController will handle
User
objects by@MessageMapping
and transform toHello
messages then use@SendTo
returns the messages to '/topic/hi' through the brokerChannel.
...
@MessageMapping("/hello")
@SendTo("/topic/hi")
public Hello greeting(User user) throws Exception {
return new Hello(...);
}
...
- The Simple Broker broadcasts messages to subscribers through the response channel.
2. Server side
In server side, we use SockJS and STOMP for our application.
What is SockJS?
-> SockJS lets applications use a WebSocket API but falls back to non-WebSocket alternatives when necessary at runtime, without the need to change application code.
Simple Java configuration to enable SockJS and Stomp in Spring application:
More at:
WebSocket – Create Spring WebSocket Application with SpringBoot + SockJS + STOMP
Top comments (0)