Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebSocket dynamically add and remove Endpoints

I have created this Websocket project Spring Websocket and it works really fine. I will introduce this example in my project. There I have the requirement that (chat-) groups can dynamically be created or removed/destroyed.

In my WebsocketConfig- class endpoints can be added statically by:

registry.addEndpoint("/hello").withSockJS(); (also see below)

Is there any possibility to add endpoints dynamically? My usecase is that I have companies and employees which belong to one or more companies:

         n     m  (m:n relation)

company <--------> employees

and companies can be created dynamically (by clicking a button "create"). Then employees, which registered before can be added to company. So this means that if a company is created (and minimim 2 employees are added to company) than an endpoint should be added.

I would be glad for any helpful answer in this direction. Thanks a lot!

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
    // Prefix for messages FROM server TO client
    config.enableSimpleBroker("/topic");
    // Prefix for messages FROM client TO server
    config.setApplicationDestinationPrefixes("/app");
    // /app wird beim client - sendName verwendet: stompClient.send("/app/hello", {}, JSON.stringify({ 'name': name
    // }));
}

@Override
public void registerStompEndpoints(final StompEndpointRegistry registry) {
    registry.addEndpoint("/hello").withSockJS();
    }
}

[Edit] Send message to more than one client but not to all. This is my current code below. Send to all with the same id works fine but I don't know how to send Message to e.g. 4 clients. Thanks for help!

@MessageMapping("/chat/{institutionId}")
public void greeting(@DestinationVariable String institutionId, final GreetingHelloMessage message) throws Exception {
    final Greeting greeting = new Greeting(institutionId, "Hello " + institutionId + " - " + message.getName());
    simpMessagingTemplate.convertAndSend("/topic/chat/" + institutionId, greeting);
}
like image 507
quma Avatar asked Sep 29 '15 12:09

quma


1 Answers

You should have a look in the direction of path parameters.

There is no need for using differnt endpoints for every chat if you can use a construct like localhost:8080/chat/{GROUP_NAME}.

like image 138
Christian Schmidt Avatar answered Nov 15 '22 10:11

Christian Schmidt