I have been working on a chat application using Spring Boot Websocket, STOMP, and stompjs. Below is a snippet of my JavaScript 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>
Here is the 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;
}
In the code snippet, you can observe that I am returning the message. Is there a way in the JavaScript side to receive this object, maybe something like this:
stompClient.send("/app/chat.sendMessage", {}, JSON.stringify(chatMessage), callback);