0

I'm using Spring WS STOMP to handle server to client notifications.
I'm using an external broker: RabbitMQ.

As far as I read, Spring forwards /topic messages to broker.
By other hand /app messages are handle directly from Spring Controller and the reply is sent just to the requester.

My js client has to subscribe to a notification service that inform the user about the status of a service.

The message broker sends the STOMP message for each status change. But at the client subscription I need to initilize the information with the current status and to reply just to this client without involving the broker.
Then I tought /topic/... is the message for broadcasting the status change, and /app/... is the message to initialize the client status at client subscription without involving the broker.

This is my controller to reply with service status at subscription:

@SubscribeMapping("/servicestatus")
public Status subscriptionStatus() {
    return checkService.getStats();
}

This is the js code:

stompClient.subscribe("/app/servicestatus", function (msg) { alert("Status change: "+msg); });

This client subscription works only for the very first initialization, if the broker sends a status update, the client doesn't receive it.
Then I tried to switch it to topic:

stompClient.subscribe("/topic/servicestatus", function (msg) { alert("Status change: "+msg); });

Now it works for updates from broker, but it doesn't work at the client subscription because @SubscribeMapping("/servicestatus") is not reached.

What I'm missing? May I subscription to both /topic/servicestatus and /app/servicestatus with the same function?

I read this question: Does Spring @SubscribeMapping really subscribe the client to some topic? But I didn't find a clear solution.

Tobia
  • 9,165
  • 28
  • 114
  • 219

1 Answers1

0

Finally I found a solution, I have to use @MessageMapping instead of @SubscribeMapping and add a send by js client command after subscription.

Server side:

@MessageMapping("/servicestatus")
public Status subscriptionStatus() {
    //this reply will be handled by stompClient.subscribe("/topic/servicestatus" callback function
    return checkService.getStats();
}

JS Client side:

stompClient.subscribe("/topic/servicestatus", function (msg) { alert("Status change: "+msg); });
stompClient.send("/app/servicestatus");
Tobia
  • 9,165
  • 28
  • 114
  • 219