There is currently no way to pass parameters to @SendTo
/ @SendToUser
, placeholders on @MessageMapping
are not available in @SendTo
/ @SendToUser
. There's an open Jira for that, keep an eye on it.
In the meantime, use the SimpMessagingTemplate
:
@Autowired
private SimpMessagingTemplate simpMessagingTemplate;
@MessageMapping("/hello/{id}")
public void greetingMultichannel(HelloMessage message, @DestinationVariable("id") Long id) throws Exception {
this.info();
Thread.sleep(300); // simulated delay
simpMessagingTemplate.convertAndSend("/topic/greetings/" + id, new Greeting(" Hello, " + message.getName() + "!"));
}
Update 8 September 2015:
As of Spring 4.2, destination variable placeholders can be used in @SendTo / @SendToUser
. This is now possible:
@MessageMapping("/hello/{id}")
@SendTo("/topic/greetings/{id}")
public Greeting greetingMultichannel(HelloMessage message) throws Exception {
this.info();
Thread.sleep(300); // simulated delay
return new Greeting(" Hello, " + message.getName() + "!");
}