4

I want to send notification to specific user. My client side code is:

stompClient.connect({},function (frame) {
   stompClient.subscribe('user/queue/notification', function(response){
     alert(angular.fromJson(response.body));
   });

Here's my server configuration:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer{

@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
    config.enableSimpleBroker("/queue");
}


@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/myWebSocketEndPoint")
            .setAllowedOrigins("*")
            .withSockJS();
    }
}

Here's my server message-sender:

@Component
public class MenuItemNotificationSender {
    public void sendNotification(MenuItemDto menuItem, String username) {
        String address = "/queue/notification";
        System.out.println(username);
        messagingTemplate.convertAndSendToUser(username,address, menuItem);
    }
}

The messages are sent correctly, to the correct username, but why client doesn't receive them?

Catechacha
  • 123
  • 4
  • 13
  • 1
    you js client subscribed to `user/queue/notification` but you send message to `String username`. does it have something in common? – Sergii Getman Feb 02 '17 at 11:23
  • 1
    I'm not sending to String "username", i use convert and send to user which automatically should add to my address the session id of the user identified by username. The string username contains the username of a user – Catechacha Feb 02 '17 at 12:00
  • 3
    http://stackoverflow.com/questions/22367223/sending-message-to-specific-user-on-spring-websocket – Catechacha Feb 02 '17 at 12:02
  • 1
    you've subscribed to 'user/queue/notification' that is hardcode string. it will process only 'user/queue/notification', not a 'user123/queue/notification' or 'user456/queue/notification' – Sergii Getman Feb 02 '17 at 12:47
  • 1
    Maybe you have configured st wrong. Does client receive broadcast messages: "messagingTemplate.convertAndSend(address, menuItem); ? – Martin Tlachač May 04 '17 at 13:38
  • Maybe this will help: https://jira.spring.io/browse/SPR-11496 – Martin Tlachač May 04 '17 at 13:42
  • here are some working examples which might help: https://github.com/rstoyanchev/spring-websocket-portfolio.git – Martin Tlachač May 04 '17 at 13:45
  • subscribe path needed to start with a slash `/user/queue/notification` ? – Jian Jason Jul 27 '20 at 05:30

0 Answers0