Search code examples
spring-websocket

Spring websockets and group messages


I would like to implement a chat using Spring websockets. What will be the best way to implement groups?

  • Register a new UserDestinationMessageHandler for URLs like /group/{groupname}/queue/* and create a new UserDestinationResolver. Are there any examples for this?
  • Make the controller implement ApplicationListener<SessionConnectedEvent>. But how can I get the session attributes from the event?

In both options I was taking into account that I will register the group to which the user belongs into session attributes.


Solution

  • I ended up creating a destination like "/topic/group/{name}/message" managed by message broker and have user subscribed to it. To send a message you can either send it to the same destination or you can send it to a destination like "/app/message" and have it forward it from there. The latter option has the advantage that you can filter the message.