2

I read Spring docs about user destinations. I would like to use convertAndSendToUser method to send a message only to a particular user.

This is the java code:

@Controller
public class WebsocketTest {

    @Autowired
    public SimpMessageSendingOperations messagingTemplate;

    @PostConstruct
    public void init(){
        ScheduledExecutorService statusTimerExecutor=Executors.newSingleThreadScheduledExecutor();
        statusTimerExecutor.scheduleAtFixedRate(new Runnable() {                
            @Override
            public void run() {
                messagingTemplate.convertAndSendToUser("myuser","/queue/test", new Return("test"));
            }
        }, 5000,5000, TimeUnit.MILLISECONDS);
    }
}

This is the client js code:

var socket = new WebSocket('ws://localhost:8080/hello');
            stompClient = Stomp.over(socket);            
            stompClient.connect("myuser","mypass", function(frame) {
                setConnected(true);
                console.log('Connected: ' + frame);
                stompClient.subscribe('/topic/greetings', function(greeting){
                    showGreeting(JSON.parse(greeting.body).value);
                });
                stompClient.subscribe('/user/queue/test', function(greeting){
                    showGreeting(JSON.parse(greeting.body).value);
                });
            });

First of all is it correct to conside the user value this one?

stompClient.connect("myuser",...

Why this test doesn't work? This user did not receveive any message. If I switch destination to /topic/greetings and change method to convertAndSend() this works, but obviously as broadcast and not only to particular user as requested.

Little update

I tried to setup a reply to single user with this code:

    @MessageMapping("/hello")
    @SendToUser("/queue/test")
    public Return test(){
        return new Return("test");
    }

This works, this is not what I need becase this reply only to a message from client. Seems that I cannot use convertAndSendToUser() for unsollicited messaged from server.

Tobia
  • 9,165
  • 28
  • 114
  • 219

2 Answers2

1

Subscribing to /user/queue/test on the client side and using convertAndSendToUser to send a message to the /topic/test topic looks about right.

Now how are you getting the "myUser" value?

You can get this value from either request.getUserPrincipal() during the handshake, or inject the Principal when receiving a message; the sessionId works as well here. You can check this commit for more details on the matter.

Brian Clozel
  • 56,583
  • 15
  • 167
  • 176
1

Maybe its late to answer the question but for the people who may face similar issue, here is what I was missing: The websocket URL which you are connecting to should also be a secured URL. If its not then there is no mapping between the user and the session and hence you are not able to send message to the user using convertAndSendToUser().

So, for e.g. you are connecting to the endpoint with URL pattern /hello then it should be a secured one.

edeesan
  • 326
  • 3
  • 16