WebSocket is "TCP over web" and STOMP is over WebSocket, like HTTP over TCP
https://spring.io/guides/gs/messaging-stomp-websocket/ (annotated)
add spring-boot-starter-parent
dependencies
spring-boot-starter-websocket
messages (incoming, outgoing)
body is a JSON
create a POJO as model of the message
controller
@Controller
@org.springframework.messaging.handler.annotation.MessageMapping("/hello") - end point for client to send to
@org.springframework.messaging.handler.annotation.SendTo("/topic/greetings") (end point for client to subscribe)
configure Spring
@org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker
@Override public void configureMessageBroker(MessageBrokerRegistry config)
config.enableSimpleBroker("/topic");
enable simple memory based broker
destination prefix with /topic - for client to subscribe
config.setApplicationDestinationPrefixes("/app");
add "/app" prefix for messages heading to @MessageMapping annotated methods, so "/app/hello" maps @MessageMapping("/hello") method
@Override public void registerStompEndpoints(StompEndpointRegistry registry)
registry.addEndpoint("/gs-guide-websocket").withSockJS();
enables SockJS fall back options in case WebSocket is not available
create browser client
dependencies
<script src="/webjars/sockjs-client/sockjs.min.js"></script>
<script src="/webjars/stomp-websocket/stomp.min.js"></script>
app.js
var socket = new SockJS('/gs-guide-websocket');
stompClient = Stomp.over(socket);
stompClient.connect({}, function (frame) {...});
stompClient.subscribe('/topic/greetings', function (greeting) {...});
stompClient.disconnect();
stompClient.send("/app/hello", {}, JSON.stringify({'name': $("#name").val()}));
https://docs.spring.io/spring-security/site/docs/current/reference/html/websocket.html