tomson
tomson

Reputation: 171

Sending messages to specific user with Spring Websocket

I set up a WebSocket with spring decribed in this tutorial: https://spring.io/guides/gs/messaging-stomp-websocket/. What i need is that my Server send out a message to specific users every 5 seconds. So i first made this:

@Autowired
private SimpMessagingTemplate template;

@Scheduled(fixedRate = 5000)
public void greet() {
    template.convertAndSend("/topic/greetings", new Greeting("Bufff!"));
}

And it works. Now to send messages only to specific users i changed the following:

@Scheduled(fixedRate = 5000)
public void greet() {
   template.convertAndSendToUser("MyName","/queue/greetings", new Greeting("Bufff!"));
}

Adding queue in WebSocketConfig.java:

public void configureMessageBroker(MessageBrokerRegistry config) {
    config.enableSimpleBroker("/topic","/queue");
    config.setApplicationDestinationPrefixes("/app");
}

Change Annotation in GreetingController.java:

@MessageMapping("/hello")
@SendToUser("/queue/greetings")
public Greeting UserGreeting(HelloMessage message, Principal principal) throws Exception {
    Thread.sleep(1000); // simulated delay
    return new Greeting("Hello, " + HtmlUtils.htmlEscape(message.getName()) + "!");
}

And change connect-function in app.js:

var socket = new SockJS('/gs-guide-websocket');
stompClient = Stomp.over(socket);
stompClient.connect({}, function (frame) {
    setConnected(true);
    console.log('Connected: ' + frame);
    stompClient.subscribe('user/queue/greetings', function (greeting) {
        showGreeting(JSON.parse(greeting.body).content);
    });
});

The server uses spring-boot-security and i asured if MyName is the right Name by looking up all users with SimpUserRegistry https://stackoverflow.com/a/32215398/11663023). But unfortunately my code is not working. I already tried this Sending message to specific user using spring but i do not want Spring to distinguish between sessions but users. I also looked at this Sending message to specific user on Spring Websocket but it did not help because the link doesn't work.

That is my console-log:

2020-01-20 17:08:51.352 DEBUG 8736 --- [nboundChannel-3] .WebSocketAnnotationMethodMessageHandler : Searching methods to handle SEND /app/hello session=kvv0m1qm, lookupDestination='/hello'
2020-01-20 17:08:51.352 DEBUG 8736 --- [nboundChannel-3] .WebSocketAnnotationMethodMessageHandler : Invoking de.iteratec.iteraweb.controllers.GreetingController#UserGreeting[2 args]
2020-01-20 17:08:52.354 DEBUG 8736 --- [nboundChannel-3] org.springframework.web.SimpLogging      : Processing MESSAGE destination=/queue/greetings-userkvv0m1qm session=null payload={"content":"Hello, hey!"}
2020-01-20 17:08:54.882 DEBUG 8736 --- [MessageBroker-2] org.springframework.web.SimpLogging      : Processing MESSAGE destination=/queue/greetings-userkvv0m1qm session=null payload={"content":"Bufff!"}
2020-01-20 17:08:59.883 DEBUG 8736 --- [MessageBroker-4] org.springframework.web.SimpLogging      : Processing MESSAGE destination=/queue/greetings-userkvv0m1qm session=null payload={"content":"Bufff!"}

Did I miss a change?

Upvotes: 5

Views: 6164

Answers (1)

Peter Lustig
Peter Lustig

Reputation: 1701

Can you see that the client subscribes successfully to your endpoint?

I think you are missing the first / in the client code 'user/queue/greetings' should be '/user/queue/greetings'

Upvotes: 4

Related Questions