title: Can a WebSocket be connected to a Socket Server?
body: <p>I want to implement a simple WebSocket server using PHP and nginx, and am under the following impression:</p>
  <ol>
  <li>Once a Websocket has gone through the &quot;Protocol Switch&quot; described in <a href="https://www.rfc-editor.org/rfc/rfc6455" rel="nofollow noreferrer">RFC 6455</a>, it can communicate with any standard (non-&quot;Web&quot;) socket server.</li>
  <li>According to the <a href="http://nginx.org/en/docs/http/websocket.html" rel="nofollow noreferrer">nginx manual</a>, nginx can perform the aforementioned &quot;Protocol Switch&quot;.</li>
  </ol>
  <p>With this in mind, I tried the following simple implementation consisting of a JavaScript client, an nginx configuration, and a PHP server (see below).</p>
  <p>Results:</p>
  <ul>
  <li>The PHP server receives the WebSocket HTTP header containing <code>Connection: upgrade</code>, <code>Sec-WebSocket-Key: ***</code> and similar fields. I assume this is good.</li>
  <li>However, the <code>onopen</code> event is never triggered on the client, and this is where I'm stuck.</li>
  </ul>
  <p>Questions:</p>
  <ul>
  <li>Have I misunderstood some details, or maybe the entire concept of how this works?</li>
  <li>How can I make my code examples work?</li>
  </ul>
  <p><strong>The Javascript client:</strong></p>
  <pre><code>function socket() {
   
    var ws = new WebSocket(&quot;ws://socket/hello/&quot;);
    
    ws.onopen = function() {
      ws.send(&quot;Hello, World!&quot;);
    };
    
    ws.onmessage = function(event) { 
      console.log(event.data);
    };
    
  }
  
  socket();
  </code></pre>
  <p><strong>The nginx configuration:</strong></p>
  <pre><code>  server {
      location /hello/ {
        proxy_pass http://localhost:10000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection &quot;upgrade&quot;;
      }   
      server_name socket;
      root socket;
    }
  </code></pre>
  <p><strong>The PHP server:</strong></p>
  <p>(Implemented exactly as described in Example #1 in the <a href="http://php.net/manual/en/sockets.examples.php" rel="nofollow noreferrer">PHP manual</a>, only changing <code>$address</code> to <code>127.0.0.1</code>.)</p>