Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-17228

Spring WebSocket Stomp tomcat cluster with user destinations sends message to wrong subscriber.

    XMLWordPrintable

    Details

      Description

      Tomcat uses AtomicLong for generating id for web socket sessions

      private static AtomicLong ids = new AtomicLong(0);
      

      at

      org.apache.tomcat.websocket.WsSession;
      

      then the id transferred to Spring WebSocket package

      public void initializeNativeSession(Session session) {
      		super.initializeNativeSession(session);
      
      		this.id = session.getId();
      

      at

      org.springframework.web.socket.adapter.standard.StandardWebSocketSession;
      

      In case of user destination subscriptions

      stompClient.subscribe("/user/queue/events", callback, {  });
      

      controller class

      @MessageMapping("/thread")
      @SendToUser(destinations = "/queue/events", broadcast = false)
      public String handleThread(String rt) {
         return new Long(Thread.currentThread().getId()).toString();
      }
      

      will pass message to user queue on RabbitMQ with name "events-user0".

      Sending message to "/app/thread"

      stompClient.send("/app/thread", {priority: 9}, "Hello, STOMP");
      

      will trigger controller with @MessageMapping. But in case when there is the second node and other client (different login) will also subscribe to "/user/queue/events" and its session id is the same as on the first node (because names just simple sequences ) The second user will receive the message instead of the first user, because first node created RibbitMQ queue with name "events-user0" and the second node subscribed to the same queue.

      The generated unique ids for the WebSocket session or different names for Broker queues can handle this issue.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              rstoya05-aop Rossen Stoyanchev
              Reporter:
              Maksim Orlov Maksim Orlov
              Last updater:
              Spring Issuemaster
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Days since last comment:
                1 year, 40 weeks, 6 days ago