The goal is to make multy-room chat using web interface and Spring framework. Lookes like STOMP over SockJS is the best combo for such project. STOMP's destination based subscription resolves all broadcasting issues. But if user uses 100 (for example) chats at once I need to send 100 subscription requests from web client each time he logs in.
So I'm looking for alternative one-request solution. Let me to consolidate questions:
1) Is there a way to make one client-side STOMP request for several subscriptions at once? If it is possible to make such request using other JS library - then I'll be glad to try it.
2) Is there a way to initiate subscriptions from Spring backend side? It would be great to register several destination message queries for one client on server side - I can use special request for that or do that during logging in.
3) Any other suggestions about this issue? Again: I'm glad to try other trendy tech as a last resort.
The code beloiw is the most simple echo service. I'm just testing this protocol and technology.
Basic client code:
window.onload = function () {
window.s = new SockJS("http://localhost:8080/portfolio");
window.s.onopen = function () {
window.stompClient = Stomp.over(window.s);
stompClient.connect('admin', 'admin', function(frame) {
console.log('Connected: ', frame);
stompClient.subscribe('/topic/echo', function(messageOutput) {
console.log(messageOutput.body);
})}, function(e){console.log("Fail My: ", e);})();
};
};
STOMP config:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/portfolio").setAllowedOrigins("*").withSockJS()
.setClientLibraryUrl( "https://cdn.jsdelivr.net/npm/[email protected]/dist/sockjs.min.js" );
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker("/topic/");
registry.setApplicationDestinationPrefixes("/app");
}
}
STOMP controller:
@Controller
public class GreetingController {
@MessageMapping("/greetings")
@SendTo("/topic/echo")
public String handleMessage(@Payload String greeting) {
System.out.println("[received]:" + greeting);
return "[echo]: " + greeting;
}
}