8

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.

Sunny Agarwal
  • 1,451
  • 4
  • 18
  • 36

4 Answers4

9

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))
Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • How can I get `sessionId` if my method is annotated with `@RequestMapping()`, in this method after some job done I want to notify other users, thank you. – Shantaram Tupe Dec 04 '17 at 05:44
  • Please, start a new SO question – Artem Bilan Dec 04 '17 at 06:11
  • please have a look at [How to subscribe using Sping Websocket integration on specific userName(userId) + get notifications from method anotated with @RequestMapping?](https://stackoverflow.com/q/47628608/3425489) – Shantaram Tupe Dec 04 '17 at 07:44
3

Spring doesn't have a clear document, I tried many different way, only below code works for me.

SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create();
accessor.setHeader(SimpMessageHeaderAccessor.SESSION_ID_HEADER, sessionId);
messagingTemplate.convertAndSendToUser(sessionId, destination, payload, accessor.getMessageHeaders());
Leo Lee
  • 468
  • 5
  • 16
2

The answer above did not work for me. It turns out that with Spring 4.1.4 something slightly different is required.

The way that seems the cleanest to me looks like the following:

SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create();
headerAccessor.setSessionId(cmd.getSessionId());
headerAccessor.setLeaveMutable(true);
MessageHeaders messageHeaders = headerAccessor.getMessageHeaders();

messagingTemplate.convertAndSendToUser(cmd.getPrincipal().getName(),
       "/queue/responses", ret, messageHeaders);

The other way which worked was to explicitly add a "nativeHeaders" value to the Map sent to SimpMessagingTemplate.convertAndSendToUser(). However, this way seems to depend too much on implementation details:

Map<String, Object> headers = new HashMap<>();
headers.put("nativeHeaders", new HashMap<String, Object>());
headers.put(SimpMessageHeaderAccessor.SESSION_ID_HEADER, cmd.getSessionId());

messagingTemplate.convertAndSendToUser(cmd.getPrincipal().getName(),
        "/queue/responses", ret, headers);

The "offending code" which made setting the "simpSessionId" header and nothing else in a Map not work was in SimpMessagingTemplate.processHeaders() and MessageHeaderAccessor.getAccessor(MessageHeaders, Class requiredType).

cortextual
  • 43
  • 4
1

The simplest way send to User by SimpMessagingTemplate

@Autowired
private SimpMessagingTemplate messagingTemplate;

@MessageMapping("/getHello")
public void sendReply( MessageHeaders messageHeaders, @Payload String message, @Header(name = "simpSessionId") String sessionId){
        messagingTemplate.convertAndSendToUser(sessionId, "/queue/hello", "Hello "+ message, messageHeaders);
}
airush
  • 722
  • 9
  • 19