title: MissingSessionUser Exception in Sockjs with Spring
body: <p>I am trying to make a chat application with WebSocket. I am able to send a message to all the connected clients, and now I am trying to send a message to a single user.</p>
  
  <p>script: </p>
  
  <pre><code>var socket = new SockJS("server endpoint");
  </code></pre>
  
  <p>subscribe:</p>
  
  <pre><code>stompClient.subscribe('/topic/chat', renderMessage);
  </code></pre>
  
  <p>connection:</p>
  
  <pre><code>stompClient.connect("guest", "guest", connectCallback, errorCallback);
  </code></pre>
  
  <p>where it's sent:</p>
  
  <pre><code>stompClient.send("/app/addMessage", {}, jsonstr);
  </code></pre>
  
  <p>and in my spring config:</p>
  
  <pre><code>&lt;websocket:message-broker
          application-destination-prefix="/app"&gt;
          &lt;websocket:stomp-endpoint path="/ws"&gt;
              &lt;websocket:sockjs /&gt;
          &lt;/websocket:stomp-endpoint&gt;
          &lt;websocket:simple-broker prefix="/topic" /&gt;
          &lt;websocket:client-inbound-channel&gt;
              &lt;websocket:interceptors&gt;
                  &lt;bean class="com.websocket.interceptor.ConnectionInterceptor" /&gt;
              &lt;/websocket:interceptors&gt;
          &lt;/websocket:client-inbound-channel&gt;
      &lt;/websocket:message-broker&gt;
  </code></pre>
  
  <p>method that is trying to send to a single user:</p>
  
  <pre><code>@MessageMapping("/addMessage")
  public void addChatMessage(ChatMessage chatMessage,
          SimpMessageHeaderAccessor headerAccessor,Principal principal) throws Exception {
      chatMessageList.add(chatMessage);
      //trying to output the user but returns null
      System.out.println(headerAccessor.getUser());
      //returns null
      System.out.println(principal.getName());
      //not sending to the current user 
      template.convertAndSendToUser("user", "/topic/chat", chatMessageList);
  }
  </code></pre>
  
  <p>and in my browser, I tried to open the console and I can see that the header is <code>undefined</code>:</p>
  
  <pre><code>connected to server undefined
  &gt;&gt;&gt; SUBSCRIBE
  id:sub-0
  destination:/topic/chat
  </code></pre>
  
  <p>stack trace:</p>
  
  <pre><code>Oct 13, 2014 9:53:36 AM org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler processHandlerMethodException
  SEVERE: Unhandled exception
  org.springframework.messaging.simp.annotation.support.MissingSessionUserException: No "user" header in message
      at org.springframework.messaging.simp.annotation.support.PrincipalMethodArgumentResolver.resolveArgument(PrincipalMethodArgumentResolver.java:43)
      at org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:81)
      at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:128)
      at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:97)
      at org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.handleMatch(AbstractMethodMessageHandler.java:451)
      at org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler.handleMatch(SimpAnnotationMethodMessageHandler.java:363)
      at org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler.handleMatch(SimpAnnotationMethodMessageHandler.java:80)
      at org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.handleMessageInternal(AbstractMethodMessageHandler.java:409)
      at org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.handleMessage(AbstractMethodMessageHandler.java:345)
      at org.springframework.messaging.support.ExecutorSubscribableChannel$1.run(ExecutorSubscribableChannel.java:70)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
      at java.lang.Thread.run(Thread.java:744)
  </code></pre>
  
  <p>How can I send to a single user? How can I get all the headers in my <code>SimpMessageHeaderAccessor</code>? And how headers actually work?</p>