Search code examples
springangularspring-bootstomp

Spring STOMP - immediate response


I am implementing an Angular client that connects to a spring boot backend via STOMP. When a client connects and wants to create a "business-group", said group is created in the backend and gets a UUID; and from that moment on other clients should be able to send to and receive messages from that group.

So I am creating a topic with said business-group id in the destination - something like

@MessageMapping("/foo/group/{id}")

However I want the creator of the group to immediately receive the business group id to be able to subscribe to it himself as well. And to be able to share the id with others (user to user).

I have used raw sockets for this before, so I was able to just use the connected user's session and then send back the id to him after the had sent the create-message. But since the session handling was business-group-ID based (so that only the users in a specific group receive the messages from that group), the whole dividing user sessions by business-group-ID had to be done manually and I was looking to upgrade this to something that does that handling for me.

However I am not sure how to achieve this with spring stomp. So my question is, is there a way for the creator of such a group to immediately receive the id as an answer/response to his initial "request"? Because he can't subscribe to anything before he receives the group id.

Background:

It's basically like a chat app where a user can create a group/chatroom and then share it with others (via URL - which contains the ID); everyone that subscribes to it can then receive msgs from it and send msgs into it. But to do so the creator first needs the created ID.


Solution

  • I found a solution. This can be done via @SendToUser

    @MessageMapping("/group.create")
    @SendToUser(value="/topic/group.create", broadcast=false)
    public Response createGroup(@Payload Message message) {
      ...
      return createResponseWithId();
    }
    

    On the other end you only need to subscribe to '/user/topic/group.create'.