Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot + Websocket (SockJS)

I'm trying to make a server that notifies connected clients when changes occur. For that, I'm using Spring Boot for the server. In order to deliver notifications, each client establish a socket with the server. I used this guide : https://spring.io/guides/gs/messaging-stomp-websocket/ and it works perfectly. In this example, the client send a message over the socket and the server responds.

  1. The problem is that I can't find out a way where the server sends a message to client without having the client to send a message first!
  2. Is it possible to list all connected websockets ?

Thank you,

like image 991
nawrasg Avatar asked May 13 '15 11:05

nawrasg


People also ask

What is Spring boot SockJS?

SockJS is WebSocket emulation library. It 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, with WebSockets or without.

Is SockJS WebSocket?

SockJS – A JavaScript library to provide WebSocket-like communication between the client and server. SockJS is a library that mimics the native WebSockets API. Additionally, it will fall back to HTTP whenever a WebSocket fails to connect, or if the browser being used doesn't support WebSockets.

Does Spring boot support WebSocket?

To build the WebSocket server-side, we will utilize the Spring Boot framework which significantly speeds up the development of standalone and web applications in Java. Spring Boot includes the spring-WebSocket module, which is compatible with the Java WebSocket API standard (JSR-356).


1 Answers

My answers:

  1. The client does not need to send a message but they do have to connect and subscribe. I am actually doing that myself in an application where a browser connects and subscribes and then starts sending messages. On the server side you can Autowire a Service (or other Component) with a SimpMessagingTemplate object and then use the convertAndSend family of functions to send things to either a particular user or all subscribers. If you check out the portfolio project you can see how it is done with the price.stock topic. The client connects and subscribes and the server has a scheduled job to send to it. This service is using a MessageSendingOperations object but you can use SimpMessagingTemplate as mentioned above. I have this code in our application service:

    @Autowired
    private SimpMessagingTemplate messagingTemplate;
    
    ...
    messagingTemplate.convertAndSendToUser(userId, destination, jsonMessage);
    
  2. This question has some good information on finding all users. It seems like you need to use the events defined in the Spring documentation on STOMP context events to keep track of things yourself if you want that. Generally since this is a subscription model you may not need to know who is connected. You could also build your own topic that you send out a request for all clients to respond to and look for their posts. I have not done this myself but Rossen (one of the commentors) is one of the main authors of the project so I believe him!

Hopefully this helps. Let me know.

like image 81
Rob Baily Avatar answered Oct 11 '22 20:10

Rob Baily