In this great answer https://stackoverflow.com/a/27161986/4358405 there is an example of how to use raw Spring4 WebSockets without STOMP subprotocol (and without SockJS potentially).
Now my question is: how do I broadcast to all clients? I expected to see an API that I could use in similar fashion with that of pure JSR 356 websockets API: session.getBasicRemote().sendText(messJson);
Do I need to keep all WebSocketSession
objects on my own and then call sendMessage()
on each of them?
I found a solution. In the WebSocket handler, we manage a list of WebSocketSession and add new session on afterConnectionEstablished function.
When we need to broadcast, just enumerate through all session in list sessions and send messages.
Hope this help!
As far as i know and can gather from the documentation here you can't broadcast using the
WebSocketHandler
.Instead you should use
Stomp
overWebSocket
configured by aWebSocketMessageBrokerConfigurer
as described here.Use a
SimpMessagingTemplate
anywhere in your code to send messages to subscribed clients as described here