i’m creating a chat application with spring boot websocket, STOMP and stompjs, here is my js code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/sockjs-client/1.1.4/sockjs.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/stomp.js/2.3.3/stomp.min.js"></script>
<script>
//some code
//send message
stompClient.send("/app/chat.sendMessage", {}, JSON.stringify(chatMessage));
</script>
Backend code:
@MessageMapping("/chat.sendMessage")
public MessageDTO sendMessage(@Payload MessageDTO chatMessage) {
//HERE
if(!validatorService.validate(chatMessage)) {
throw new RuntimeException("invalid");
}
messagingTemplate.convertAndSend("/topic/public", chatMessage);
return chatMessage;
}
You can see if the message is not valid, the exception is thrown, and the message is not sent to /topic/public. In js side is there any way to know that the message is processed successfully like
stompClient.send("/app/chat.sendMessage", {}, JSON.stringify(chatMessage), successCallback, errorCallback);