Ihor Kosandiak
1 min readDec 6, 2017

--

Yes, you can easily achieve this by creating some sort of private channels(rooms). To do this in the method

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.setApplicationDestinationPrefixes("/app")
.enableSimpleBroker("/chat", "/privateRoom");
}

just add another mapping, for example “/privateRoom”. And to send messages to single, or couple chosen users you may want to create room with the unique ID that both of the users will be familiar with. Let’s say id will be “room1”.

So, imagine, when your users go to the room1, they subsribe to “/privateRoom/room1” channel.

In Spring Boot app we need to add one more mapping in the controller:

@MessageMapping("/{roomId}")
private void sendMessageTpPrivateRoom(String message, @DestinationVariable ObjectId roomId) throws IOException {
this.template.convertAndSend("/privateRoom/" + roomId, message);
}

Here we dynamically change the room ids based on where user sent messages. So if user goes to “room7” and send message → we will send it to all users currently subscribed to “/privateRoom/room7” channel. And so on.

And you client will need to send messages to something like:

sendMessage(message){
this.stompClient.send("/app/room1" , {}, message);
}

And that’s it. This message will receive only users who subscribed to your private room with id “room1”.

--

--

Ihor Kosandiak
Ihor Kosandiak

Written by Ihor Kosandiak

Java Software Developer. Passionate about new technologies. Sharing own experience with others.

No responses yet