Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebSocket with Sockjs & Spring 4 but without Stomp

Is there a way to use WebSockets with SockJS client and Spring 4 server but not using STOMP?

Based on this tutorial from Spring's website, I know how to set up a WebSocket based application using Stomp and Spring 4. On the client side, we have:

     var socket = new SockJS('/hello');         stompClient = Stomp.over(socket);         stompClient.connect({}, function(frame) {             setConnected(true);             console.log('Connected: ' + frame);             stompClient.subscribe('/topic/greetings', function(greeting){                 showGreeting(JSON.parse(greeting.body).content);             });         }); 

And on the server side, we have the following in the controller:

@MessageMapping("/hello") @SendTo("/topic/greetings") public Greeting greeting(HelloMessage message) throws Exception {     Thread.sleep(3000); // simulated delay     return new Greeting("Hello, " + message.getName() + "!"); } 

Now, I understand that @MessageMapping("/hello") ensures that if a message is sent to a destination "/hello", then the greeting() method will be called. And since the stompClient is subscribed to "/topic/greetings", the @SendTo("/topic/greetings") will send the message back to the stompClient.

But the problem with the above is that stompClient is a Stomp object. And I want to simply use sock.send('test'); and have it delivered to my server's destination. And I want to do @SendTo("myownclientdestinationmap"), I can receive it by

sock.onmessage = function(e) {      console.log('message', e.data);  }; 

So, any way to do this with Spring 4, SockJS and without Stomp? Or does Spring 4 WebSocket only supports Stomp?

like image 331
BlueChips23 Avatar asked Nov 26 '14 20:11

BlueChips23


People also ask

What is SockJS in WebSocket?

Summary. SockJS is a browser JavaScript library that provides a WebSocket-like object. SockJS gives you a coherent, cross-browser, Javascript API which creates a low latency, full duplex, cross-domain communication channel between the browser and the web server.

Does WebSocket work with load balancer?

The load balancer knows how to upgrade an HTTP connection to a WebSocket connection and once that happens, messages will travel back and forth through a WebSocket tunnel. However, you must design your system for scale if you plan to load balance multiple WebSocket servers.

Can I use WebSocket with REST API?

Yes. You can use REST over WebSocket with library like SwaggerSocket.

Does spring boot support WebSocket?

Spring Boot includes the spring-WebSocket module, which is compatible with the Java WebSocket API standard (JSR-356). Implementing the WebSocket server-side with Spring Boot is not a very complex task and includes only a couple of steps, which we will walk through one by one.


1 Answers

Spring supports STOMP over WebSocket but the use of a subprotocol is not mandatory, you can deal with the raw websocket. When using a raw websocket, 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:

public class GreetingHandler extends TextWebSocketHandler {      @Override     public void handleTextMessage(WebSocketSession session, TextMessage message) {         Thread.sleep(3000); // simulated delay         TextMessage msg = new TextMessage("Hello, " + message.getPayload() + "!");         session.sendMessage(msg);     } } 

And then add your handler to the registry in the configuration (you can add more than one handler and use SockJS for fallback options):

@Configuration @EnableWebSocket public class WebSocketConfig implements WebSocketConfigurer {      @Override     public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {         registry.addHandler(greetingHandler(), "/greeting").withSockJS();     }      @Bean     public WebSocketHandler greetingHandler() {         return new GreetingHandler();     } } 

The client side will be something like this:

var sock = new SockJS('http://localhost:8080/greeting');  sock.onmessage = function(e) {     console.log('message', e.data); } 
like image 129
Sergi Almar Avatar answered Oct 08 '22 02:10

Sergi Almar