Reputation: 501
I am using grails/groovy so excuse the odd syntax, i am also new to using websockets so please let me know if i am going about this in the wrong way:
Using spring websockets i am able to send messages to certain subscribed users via
SimpMessagingTemplate brokerMessagingTemplate
users.each {
brokerMessagingTemplate.convertAndSendToUser(it.id,"/topic/path",data)
}
However, i want to send messages only to subscribed users have passed to the server a certain value/id over and above their user id. A connection is initialised on wepage load so i figured perhaps that i could add a STOMP header value which passes this information to the server, and the server only sends messages to connections which match this.
var socket = new SockJS("/url/stomp");
var client = Stomp.over(socket);
var headers = {'additionalId': additionalId};
client.connect({}, function() {
client.subscribe("/user/topic/path", function (data) {
}, headers);
firstly, i dont know whether adding a header value is the right way to do this, and secondly im not sure how to make the SimpMessagingTemplate send to those that have specifically provided the additional Id in the header.
Upvotes: 0
Views: 460
Reputation: 2340
Instead of using a header you can use DestinationVariable
as so:
brokerMessagingTemplate.convertAndSend("/topic/something.${additionalId}".toString(), data)
and use
@MessageMapping("/something.{additionalId}")
protected String chatMessage(@DestinationVariable String additionalId, Principal principal, String data) { ... }
Additionally you may want to limit who subscribe to a specific /something.{additionalId}
by implementing a TopicSubscriptionInterceptor()
where you can validate the Principal
Upvotes: 1