9

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?

Community
  • 1
  • 1
TMG
  • 2,620
  • 1
  • 17
  • 40
  • Hi, Have the same question you have: how do I broadcast to all clients? did you have/got the answer for this ? thanks for you help. – Delli Kilari May 06 '17 at 00:43

2 Answers2

6

I found a solution. In the WebSocket handler, we manage a list of WebSocketSession and add new session on afterConnectionEstablished function.

private List<WebSocketSession> sessions = new ArrayList<>();

synchronized void addSession(WebSocketSession sess) {
    this.sessions.add(sess);
}

@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
    addSession(session);
    System.out.println("New Session: " + session.getId());
}

When we need to broadcast, just enumerate through all session in list sessions and send messages.

for (WebSocketSession sess : sessions) {
        TextMessage msg = new TextMessage("Hello from " + session.getId() + "!");
        sess.sendMessage(msg);
}

Hope this help!

novax
  • 481
  • 4
  • 7
  • I'm aware of that solution - this looks exactly like an implementation of the solution I've already mentioned in my question. I rather wanted to know if it has any downsides and there is a better approach or this is actually the best solution. – TMG Nov 18 '16 at 12:06
  • So what solution did u come up with @TMG ? Im doing as same solution here, did it work fine for you? – Sepehr GH Feb 03 '18 at 09:39
  • @SepehrGH At that time I did not find anything better. I have not faced any particular issues with this solution, but don't take it as my endorsement. I have stopped working on that project soon after and I can't really tell if it worked good or not. – TMG Feb 03 '18 at 21:47
  • @TMG thanks for the information. I'm using this approach in our messaging system (self-developed protocol)... think it will work fine – Sepehr GH Feb 04 '18 at 06:22
0

As far as i know and can gather from the documentation here you can't broadcast using the WebSocketHandler.

Instead you should use Stomp over WebSocket configured by a WebSocketMessageBrokerConfigurer as described here.

Use a SimpMessagingTemplate anywhere in your code to send messages to subscribed clients as described here

s.ijpma
  • 930
  • 1
  • 11
  • 23
  • 2
    What exactly do you mean by "you can't do this"? I can't use a broadcast API because it doesn't exist, or I can't keep a `List` because of some other implications that I'm not aware of ? – TMG Nov 25 '15 at 19:31
  • i tried SimpMessagingTemplate but it is not able to send, there are no errors, please help – Afroz Shaikh Aug 12 '16 at 09:15