I am working with Spring websocket implementation. For sending a message to clients, there are two ways:
1) Using @SendToUser
annotation
2) Using convertAndSendToUser
method of SimpMessagingTemplate
@SendToUser
takes a boolean parameter called broadcast
which if set to false publishes the message to the current session. Is there a way I can have this behaviour in SimpMessagingTemplate
.
If we take a look to the SendToMethodReturnValueHandler
source code, we'll see:
if (broadcast) {
this.messagingTemplate.convertAndSendToUser(user, destination, returnValue);
}
else {
this.messagingTemplate.convertAndSendToUser(user, destination, returnValue, createHeaders(sessionId));
}
So, what you need for your use-case just use that overloaded convertAndSendToUser
and provide a Map
with `sessionId:
messagingTemplate.convertAndSendToUser(user, destination, payload,
Collections.singletonMap(SimpMessageHeaderAccessor.SESSION_ID_HEADER, sessionId))