Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic websocket with Spring without STOMP and SockJS

I have implemented the following websocket endpoint

@MessageMapping("/socket/{myId}/")
@SendTo("/queue/myqueue")
public MyObject getObject(@DestinationVariable String myId) throws Exception {

    return new MyObject("MyId:" + myId);
}

Now how can I send message to that endpoint from one of my service.java class? There will be front-end client as well, which will read the message from websocket once the service.java class's method send some message to websocket endpoint. I am a little confused that how can I do that?

Any help would be appreciated

like image 490
Ijaz Avatar asked Oct 17 '25 18:10

Ijaz


1 Answers

When using a raw websocket(without STOMP), the message sent lacks of information to make Spring route it to a specific message handler method (we don't have any messaging protocol), so instead of annotating your controller, you'll have to implement a WebSocketHandler by extending TextWebSocketHandler public void handleTextMessage(WebSocketSession session, TextMessage message){ }

Checkout an example here spring boot websocket without STOMP and SockJs

like image 97
Dhiraj Ray Avatar answered Oct 20 '25 08:10

Dhiraj Ray