0

With the example provided by spring.io and http://www.baeldung.com/websockets-spring is helped to create a websocket connection between client and server, but my case is. - Some one is creating message from UI that is passed to Spring controller (Separate controller). - From this controller I need to notify/send/broadcast this message to all connected clients. - How the message is passed to handler from controller where message is received. I also refereed WebSocket with Sockjs & Spring 4 but without Stomp here and the same question is posted.

Can some one help me here, Thanks in advance !!

Community
  • 1
  • 1
Delli Kilari
  • 988
  • 3
  • 14
  • 31

1 Answers1

1

I actually write for Baeldung too and am currently writing a small article about how to add security to websockets in Spring! There are just a few steps you need to do to get this all working!

Backend-wise (since you said the UI was already done or being built, I'll just focus on the backend here), it really involves three parts: (1) the necessary POJO's, the controller, and the configuration.

Your POJO's will be very simple - here we just use Greeting and Message which specify a name and basic text data type (I'll skip over this here to save space but you can see it in the resource below).

Your controller will look like this:

@Controller
public class GreetingController {

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

}

Take a look at the annotations - those are really what set this controller apart from say a normal REST controller.

And your configuration looks like this - again take a look at the annotations - particularly '@EnableWebSocketMessageBroker' - and the class 'AbstractWebSocketMessageBrokerConfigurer':

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic");
        config.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/gs-guide-websocket").withSockJS();
    }
}

A look at this great resource too: https://spring.io/guides/gs/messaging-stomp-websocket/

Adam Gerard
  • 708
  • 2
  • 8
  • 23
  • Thanks, I followed the same, I am using 2 websocket end points (One is with in the same web application and other is external), Do I need to have 2 different certificates ? – Delli Kilari May 09 '17 at 14:12
  • Yes, if the two websockets are on different domains and you want to provide WSS (SSL for websockets) on both! Also, you'd probably want to look into provide security coverage for both endpoints (meaning if you're using two Spring apps each with an exposed WS endpoint, you'll want Spring Security on each). But, depending on how you set each up, you could use the same authentication. – Adam Gerard May 10 '17 at 18:56
  • Thank Adam, that make sense. Since we have 2 end points and uses the same sockJS client and sockJS restrict creating more than 2 connections, how do I proceed with this, actually i need 3 sockJS connections from client side. – Delli Kilari May 11 '17 at 00:59
  • 1
    If your client is in Java: probably something like this: List transports = new ArrayList(2); transports.add(new WebSocketTransport(new StandardWebSocketClient())); – Adam Gerard May 11 '17 at 23:49
  • You can modify the number of transports to 2+. Honestly, I'm not sure in JavaScript since I didn't think it was capped by default. – Adam Gerard May 11 '17 at 23:50
  • Thanks Adam, I will see is there any other possibilities to implement. – Delli Kilari May 12 '17 at 13:45