I am using Spring Websocket with STOMP, Simple Message Broker.
In my @Controller
I use method-level @SubscribeMapping
, which should subscribe the clien
So having both:
does not work as you experienced (as well as me).
The way to solve your situation (as I did mine) is:
Implement an ApplicationListener
If you want to directly reply to a single client use a user destination (see websocket-stomp-user-destination or you could also subscribe to a sub-path e.g. /topic/my-id-42 then you can send a message to this subtopic (I don't know about your exact use case, mine is that I have dedicated subscriptions and I iterate over them if I want to do a broadcast)
Send a message in your onApplicationEvent method of the ApplicationListener as soon as you receive a StompCommand.SUBSCRIBE
Subscription Event Handler:
@Override
public void onApplicationEvent(SessionSubscribeEvent event) {
Message message = event.getMessage();
StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message);
StompCommand command = accessor.getCommand();
if (command.equals(StompCommand.SUBSCRIBE)) {
String sessionId = accessor.getSessionId();
String stompSubscriptionId = accessor.getSubscriptionId();
String destination = accessor.getDestination();
// Handle subscription event here
// e.g. send welcome message to *destination*
}
}