I have a problem.
I’m managing a fake dating app with my colleague in order . I’m in charge of the Db (via Java SpringBoot) and I’m trying to implement a real-time one-to-one messaging system with WebSocket.
One important thing is that I really have problems to understand how WebSocket works. Every single resource I find, gives a big chunk of the explanations as “obvious” without explaining the “why” behind those lines of code and so…
Anyway, this is the WebSocketConfig
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/queue");
config.setUserDestinationPrefix("/user");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/chat-websocket").withSockJS();
}
}
And this is the controller for the messages:
@RestController
@RequestMapping("/chat")
@MessageMapping("/send")
public ResponseEntity<?> sendMessage(@Valid @RequestBody NewMessage newMessage, Authentication auth) {
//my code here
try {
Chat chat = chatService.sendMessage(senderId, newMessage);
logger.debug("here the chat object vaffanculo:" + chat.toString());
messagingTemplate.convertAndSendToUser(
newMessage.getReceiverId().toString(), // destinatario
"/queue/messages", // coda dell'utente
chat // contenuto del messaggio
);
//other code here
}
I haven’t written the rest of code for the controller for two reasons:
-
When I transorm it in a PostMapping and I just try to send a message and store it in the database, it works.
-
When I change the annotation from @PostMapping to @MessageMapping and I try to make a request to send a message, it says that the /app/send or /app/chat/send or /chat/send or whatever, doesn’t exist.
I have tried to use Postman (since the Frontend hasn’t implemented WebSocket yet and I cannot test with it).
As I said, if I use it with a @PostMapping and I send a POST request to localhost:8080/CHAT/SEND, it works. It sends the message, that it’s stored in the database. Obviously, as a first step, I could use this method and ask the receiver to “refresh” the page every single time to receive the message (if and when we do a demo for the class) thank to the chatHistory method I have implemented. But, obviously, I would like to make it work as truly intended: a one-to-one real-time chat).
So, when I transform the annotation in @MessageMapping to make it work with WebSocket and send the message in realTime to the receiver with a POST request, it says “Path not found”. I have tried /APP/SEND, /APP/CHAT/SEND, /CHAT/SEND, /CHAT/APP/SEND but nothing works.
I know I shouldn’t TRY but I should KNOW, the problem is that I really cannot understand how to make it work.
I think the problem could be that I send a POST request to the @MessageMapping method while I should send a different type of request, but I don’t know how to do it.
Or, maybe, my websocket is not working and I don’t know where to address my requests.