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);
}
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}.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With