Multiple rooms in Spring using STOMP Multiple rooms in Spring using STOMP spring spring

Multiple rooms in Spring using STOMP


It looks like this "room" feature is actually a publish/subscribe mechanism, something achieved with topics in Spring Websocket support (see STOMP protocol support and destinations for more info on this).

With this example:

@Controllerpublic class GreetingController {  @MessageMapping("/room/greeting/{room}")  public Greeting greet(@DestinationVariable String room, HelloMessage message) throws Exception {    return new Greeting("Hello, " + message.getName() + "!");  }}

If a message is sent to "/room/greeting/room1", then the return value Greeting will be automatically sent to "/topic/room/greeting/room1", so the initial destination prefixed with "/topic".

If you wish to customize the destination, you can use @SendTo just like you did, or use a MessagingTemplate like this:

@Controllerpublic class GreetingController {  private SimpMessagingTemplate template;  @Autowired  public GreetingController(SimpMessagingTemplate template) {    this.template = template;  }  @MessageMapping("/room/greeting/{room}")  public Greeting greet(@DestinationVariable String room, HelloMessage message) throws Exception {    Greeting greeting = new Greeting("Hello, " + message.getName() + "!");    this.template.convertAndSend("/topic/room/"+room, greeting);    }}

I think taking a quick look at the reference documentation and some useful examples, such as a portfolio app and a chat app should be useful.