title: Spring WebSocket Connecting with SockJS to a different domain
body: <p>WebSockets in Spring is a rather new topic that I;m tiring to find a bit more. </p>
  
  <p>My problem is with connecting to a service from a different domain, I'm working on with Lineman building the front-end side and Spring Boot when doing the back-end side, with that I have these apps on two different ports : 8000 and 8080 on localhost. </p>
  
  <p>I had issues with the 'Access-Control-Allow-Origin' header but I have resolved it by adding a filter on the server side which added the allowed origin to the header. After this I started to get the following error on connection: </p>
  
  <pre><code>GET http://localhost:8080/socket/info 403 (Forbidden)
  AbstractXHRObject._start @ sockjs-0.3.4.js:807
  (anonymous function) @sockjs-0.3.4.js:841
  </code></pre>
  
  <p>I don't have Spring Security in the project so this is not an authorization issue, the error points to sockJS :
  that.xhr.send(payload); - where payload is never defined.I tried but couldn't find the root of the call where is may began. </p>
  
  <p>I was thinking if I need to add some additional information to either SockJS and Stomp when setting the connection, but there is not much of examples and notes in both wiki pages of this tools.</p>
  
  <p>Bellow you will find the connection JS code. </p>
  
  <pre><code>var socket = new SockJS("http://localhost:8080/socket");
  client = Stomp.over(socket);
  
  client.connect({'login': BoatsGame.userName,
                      'passcode': 'guest'},
              function (frame) {
  ....
  
  The Server Side has a MessageBroker configured :    
  
  
  @Configuration
  @EnableWebSocketMessageBroker
  public class MessageBrokerConfig extends AbstractWebSocketMessageBrokerConfigurer {
  
  @Bean
  public ServletServerContainerFactoryBean createWebSocketContainer() {
       ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
       container.setMaxTextMessageBufferSize(8192);
       container.setMaxBinaryMessageBufferSize(8192);
       return container;
  }
  
  @Override
  public void configureMessageBroker(MessageBrokerRegistry config) {
       //config.enableStompBrokerRelay("/queue", "/topic");
       config.enableSimpleBroker("/queue", "/topic","/user");
       config.setApplicationDestinationPrefixes("/BoatBattleGame");
  }
  
  @Override
  public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {
      stompEndpointRegistry.addEndpoint("/socket").withSockJS();
  }
  }
  </code></pre>
  
  <p>I Also tried setting up a MessageHandler as it has the option to set OriginAllowe when configuring, but I'm not sure how it is connected to the broker. </p>
  
  <p>Last think, this setup works correctly when running on one port. </p>