0

I have a web app that has registered users and i would like to push messages to users who have unique user ids. To be able to send a message to the clients, i need to know how to pass unique user id in the @sendto annotation

This is the annotation

@SendTo("/topic/uniqueuserid")
    public Greeting greeting(HelloMessage message) throws Exception {
        int uniqueuserid;
        Thread.sleep(1000); // simulated delay
        return new Greeting("Hello, " + message.getName() + uniqueuserid "!");
}

and this the stomp js

stompClient.subscribe('/topic/uniqueuserid', function (greeting) {
            showGreeting(JSON.parse(greeting.body).content);
        });

How can i pass a unique user id in @SendTo("/topic/uniqueuserid")

Le Qs
  • 785
  • 2
  • 9
  • 26
  • 1
    Maybe https://stackoverflow.com/questions/27047310/path-variables-in-spring-websockets-sendto-mapping –  Oct 12 '17 at 09:56

1 Answers1

3

You can use @DestinationVariable annotation in a method argument, like that:

@MessageMapping("/mychat/{uniqueUserId}")
@SendTo("/topic/{uniqueUserId}")
public Message message(@DestinationVariable("uniqueUserId") Long uniqueUserId, HelloMessage message) {
    logger.info("Unique user id: {}", uniqueUserId);
}
Pijotrek
  • 2,821
  • 1
  • 18
  • 32