title: Connecting to a websocket in Java
body: <p>I am trying to connect to the web socket <code>wss://ws-feed.gdax.com</code></p>
  
  <p>I have got this working in JavaScript (<a href="https://jsbin.com/kurigokuwu/edit?html,output" rel="nofollow noreferrer">See here</a>) but I am trying to move the connection Server side into my Spring Boot app.</p>
  
  <p>So far I have:</p>
  
  <pre><code>@SpringBootApplication
  public class DemoApplication implements CommandLineRunner {
  
      private Logger LOG = LoggerFactory.getLogger(DemoApplication.class);
  
      public static void main(String[] args) {
          SpringApplication.run(DemoApplication.class, args);
      }
  
      @Override
      public void run(String... args) throws Exception {
          WebSocketContainer container = ContainerProvider.getWebSocketContainer();
          container.setDefaultMaxTextMessageBufferSize(64*1024);
          WebSocketClient simpleWebSocketClient = new StandardWebSocketClient(container);
          List&lt;Transport&gt; transports = new ArrayList&lt;&gt;(1);
          transports.add(new WebSocketTransport(simpleWebSocketClient));
          SockJsClient sockJsClient = new SockJsClient(transports);
          WebSocketStompClient stompClient = new WebSocketStompClient(sockJsClient);
          stompClient.setMessageConverter(new MappingJackson2MessageConverter());
  
          LOG.info("Connecting To [wss://ws-feed.gdax.com]");
          StompSession session = stompClient.connect("wss://ws-feed.gdax.com", new GDAXHandler()).get();
      }
  
      private class GDAXHandler extends WebSocketHttpHeaders implements StompSessionHandler {
  
          @Override
          public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
              LOG.info("Connected");
              String payload = "{\"type\": \"subscribe\",\"channels\":[{\"name\": \"ticker\",\"product_ids\": [\"ETH-EUR\"]}]\"}";
              LOG.info("Sending [" + payload + "]");
              session.send("/", payload);
          }
  
          @Override
          public void handleException(StompSession session, StompCommand command, StompHeaders headers, byte[] payload, Throwable exception) {
              LOG.error("Exception", exception);
          }
  
          @Override
          public void handleTransportError(StompSession session, Throwable exception) {
              LOG.error("Transport Error", exception);
          }
  
          @Override
          public Type getPayloadType(StompHeaders headers) {
              return String.class;
          }
  
          @Override
          public void handleFrame(StompHeaders headers, Object payload) {
              LOG.info("Frame [" + payload + "]");
          }
      }
  }
  </code></pre>
  
  <p>If I run this app the output is:</p>
  
  <blockquote>
    <p>INFO Tomcat started on port(s): 8080 (http) </p>
    
    <p>INFO Connecting To [wss://ws-feed.gdax.com]</p>
    
    <p>INFO Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled. </p>
    
    <p>ERROR Transport Error org.springframework.messaging.simp.stomp.ConnectionLostException: Connection closed</p>
    
    <p>Caused by:
    org.springframework.messaging.simp.stomp.ConnectionLostException:
    Connection closed     at
    org.springframework.messaging.simp.stomp.DefaultStompSession.afterConnectionClosed(DefaultStompSession.java:487)
    ~[spring-messaging-4.3.13.RELEASE.jar:4.3.13.RELEASE]     at
    org.springframework.web.socket.messaging.WebSocketStompClient$WebSocketTcpConnectionHandlerAdapter.afterConnectionClosed(WebSocketStompClient.java:354)
    ~[spring-websocket-4.3.13.RELEASE.jar:4.3.13.RELEASE]     at
    org.springframework.web.socket.sockjs.client.AbstractClientSockJsSession.afterTransportClosed(AbstractClientSockJsSession.java:324)
    ~[spring-websocket-4.3.13.RELEASE.jar:4.3.13.RELEASE]     at
    org.springframework.web.socket.sockjs.client.WebSocketTransport$ClientSockJsWebSocketHandler.afterConnectionClosed(WebSocketTransport.java:172)
    ~[spring-websocket-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    ...</p>
  </blockquote>
  
  <p>It never even makes it to the <code>Connected</code> state. What am I doing wrong?</p>