20

Is it possible to create rooms with STOMP and Spring 4? Socket.IO has rooms built in, so I'm wondering if Spring has this

My code at the moment:

@MessageMapping("/room/greet/{room}")
@SendTo("/room/{room}")
public Greeting greet(@DestinationVariable String room, HelloMessage message) throws Exception {
    return new Greeting("Hello, " + room + "!");
}

It would be ideal to have @SendTo("/room/{room}")

however, I am limited to:

@SendTo("/room/room1") 
@SendTo("/room/room2")
@SendTo("/room/room3") 

etc...which is VERY VERY unideal

The client is:

stompClient.subscribe('/room/' + roomID, function(greeting){
    showGreeting(JSON.parse(greeting.body).content);
});

where roomID can be room1, room2, or room3... What If I want more rooms? It feels like such a pain right now

Dolan
  • 1,519
  • 4
  • 16
  • 36

2 Answers2

41

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:

@Controller
public 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:

@Controller
public class GreetingController {

  private SimpMessagingTemplate template;

  @Autowired
  public GreetingController(SimpMessagingTemplate template) {
    this.template = template;
  }

  @MessageMapping("/room/greeting/{room}")
  public void 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.

NoobSailboat
  • 109
  • 9
Brian Clozel
  • 56,583
  • 15
  • 167
  • 176
  • This might also be useful https://stackoverflow.com/questions/27047310/path-variables-in-spring-websockets-sendto-mapping/27055764#27055764 – Sergi Almar Feb 09 '15 at 08:21
  • any chance the rest of your source is available elsewhere? I'm just wondering what other configuration is necessary to make this work. – Jordan Mackie Jul 03 '18 at 09:11
0

You can use netty socket the implementation of socket io in java

djaber atia
  • 71
  • 1
  • 4