How to send websocket message from server to specific user only?
My webapp has spring security setup and uses websocket. I'm encountering tricky problem trying to send message from server to specific user only.
My understanding from reading the manual is from the server we can do
simpMessagingTemplate.convertAndSend("/user/{username}/reply", reply);
And on the client side:
stompClient.subscribe('/user/reply', handler);
But I could never get the subscription callback invoked. I have tried many different path but no luck.
If I send it to /topic/reply it works but all other connected users will receive it too.
To illustrate the problem I've created this small project on github: https://github.com/gerrytan/wsproblem
Steps to reproduce:
1) Clone and build the project (make sure you're using jdk 1.7 and maven 3.1)
$ git clone https://github.com/gerrytan/wsproblem.git
$ cd wsproblem
$ mvn jetty:run
2) Navigate to http://localhost:8080
, login using either bob/test or jim/test
3) Click "Request user specific msg". Expected: a message "hello {username}" is displayed next to "Received Message To Me Only" for this user only, Actual: nothing is received
simpMessagingTemplate.convertAndSendToUser(principal.getName(), "/user/reply", reply);
and when the message is sent from server it throws this exceptionjava.lang.IllegalArgumentException: Expected destination pattern "/principal/{userId}/**"
– AirspaceconvertAndSendToUser(principal.getName(), "/reply", reply);
– Scalene