2

I want to create 2 web socket endpoints. Can you tell is it possible?

What shall be the configuration in that case?

abcd
  • 95
  • 2
  • 8

1 Answers1

5

Your question does not clearly states if you're using plain websockets or STOMP messaging.

Plain websocket API

If you're using the plain websocket API, the registry API allows you to add as many websocket handlers as you want.

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(myFirstHandler(), "/myHandler1").withSockJS();
        registry.addHandler(mySecondHandler(), "/myHandler2").withSockJS();
    }

    @Bean
    public WebSocketHandler myFirstHandler() {
        return new MyFirstHandler();
    }

    @Bean
    public WebSocketHandler mySecondHandler() {
        return new MySecondHandler();
    }

}

STOMP endpoints

If you're using STOMP and would like to add several STOMP endpoints, then the API also allows you to do that (the addEndpoint method accepts a String vararg):

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/foo", "/bar").withSockJS();
}
Brian Clozel
  • 56,583
  • 15
  • 167
  • 176
  • edited my answer - please elaborate your question if this does not answer your question. – Brian Clozel Oct 06 '14 at 10:03
  • 1
    Say We have two text boxes and each of them getting populated from different sources .Is it possible to have Single WebSocketConnection that gets initiated once the user login and having multiple channels/endpoint to which user can choose to subscribe from ? – Amit_Hora Jun 16 '16 at 11:21
  • Hi @BrianClozel , I'm implementing Post and Comment module, how many subscriptions channel should I create e.g. addPost, updatePost, addComment, updateComment. Should I create individual subscription channel for all of them? This is not per user, visible for all users. – Shantaram Tupe Jun 05 '18 at 10:39