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 I return the message. In js side is there anyway to receive this object, something like
stompClient.send("/app/chat.sendMessage", {}, JSON.stringify(chatMessage), callback);
When a STOMP client sends a MESSAGE
frame it usually won't get any response back from the broker at all. However, it may get back a RECEIPT
frame if the MESSAGE
frame included the receipt
header. You aren't setting this header so you shouldn't expect any response back from the broker.
If you wanted to get some kind of application-specific response then you would need to create a subsriber in your JavaScript app and the server-side app would need to send a message to wherever that consumer was listening. This is the classic request/reply messaging pattern.