Search code examples
javascriptjavastompspring-websocketsockjs

stomp client is not getting messages/notifications when SessionSubscribeEvent is happend


I've a subscribe listener as

@Service
public class UserSubscribeEventListenerService {

    private SimpMessagingTemplate template;
    @Autowired
    private GenericService<User> userService;

    @Autowired
    public UserSubscribeEventListenerService(SimpMessagingTemplate template) {
        this.template = template;
    }

    @EventListener
    public void handleSubscribeEvent(SessionSubscribeEvent event) {
        try {
            String subscribedUser = event.getUser().getName();
            System.out.println("User is " + event.getUser().getName() + " Message is " + "GREETINGS" );
            Thread.sleep(3000);
            System.out.println("AFTER 3 seconds...." );
            template.convertAndSendToUser(subscribedUser, "/user/"+subscribedUser+ "/notify", userService.getByEmail(subscribedUser));
        } catch (MessagingException e) {
            System.out.println(e.getCause() 
                    + "\n\n"
                    + e.getCause());
            return;
        } catch (InterruptedException e) {
            System.out.println(e.getCause() 
                    + "\n\n"
                    + e.getCause());
            return;
        }
    }       
}

but client side which are bunch of JS functions for stomp given below

<script type="text/javascript">     
    //Create stomp client over sockJS protocol
    var socket = new SockJS("/ws");
    ///var socket = new SockJS("/ws");
    var stompClient = Stomp.over(socket);
     $( document ).ready(function() {
         stompClient.connect({}, function(s) {    

                stompClient.subscribe('/user/' + s.headers['user-name'] + '/notify', function(frame){
                    var messages = JSON.parse(frame.body)

                      var i;
                      var count = 0;
                      for( i in messages) {
                          var message = messages[i]; 
                          count ++;
                          if(count <= 5){
                              $('#user_message_addon').append(
                                $('<li>').append(                                       
                                      $('<span></span>').html(message.notificationMessage)
                                    ),
                                    $('</li>')      
                                )
                              );
                          }
                        }                            
                });             

            });
        });
    </script>

is not seems to work properly, not showing or say not getting messages sent by by server when ever UserSubscribeEventListenerService event is occurred I'm getting console output at client side as

Image showing console output

but I'm unable to get message/notifications.

Can any body tell me how to resolve that issue?


Solution

  • Did you try changing convertAndSendToUser arguments to:

    template.convertAndSendToUser(subscribedUser, "/notify", userService.getByEmail(subscribedUser));
    

    or

    template.convertAndSendToUser(subscribedUser, "/user/notify", userService.getByEmail(subscribedUser));
    

    I think spring changes queue name itself depending on user name. Or you can do it with convertAndSend method:

    template.convertAndSend("/"+subscribedUser+"/notify", userService.getByEmail(subscribedUser));
    

    or

    template.convertAndSend("/user/"+subscribedUser+"/notify", userService.getByEmail(subscribedUser));
    

    It depends on your websocket configuration (could you include it in your question?)