title: Websockets not working on Google App Engine
body: <p>I recently deployed a node.js server application to Google App Engine that communicates with a client application via socket.io. Accordingly to <a href="https://cloud.google.com/appengine/docs/flexible/nodejs/using-websockets-and-session-affinity" rel="noreferrer">this article</a>, websockets are now supported on App Engine. </p>
  
  <p>However, the client app is unable to connect to the server over the wss protocol. I can see the following error message in the browser console. (I removed my server domain)</p>
  
  <blockquote>
    <p>WebSocket connection to 'wss://[my server]/socket.io/?EIO=3&amp;transport=websocket&amp;sid=dbB2UgsCYhD7c1ucAAAA' failed: Error during WebSocket handshake: Unexpected response code: 400</p>
  </blockquote>
  
  <p>Socket.io then falls back to https long polling, which works well. </p>
  
  <p>Here is my app.yaml for deployment to app engine, with session_affinity set to true for long polling.</p>
  
  <pre><code>runtime: nodejs10
  
  instance_class: F2
  
  handlers:
  - url: /.*
    secure: always
    redirect_http_response_code: 301
    script: auto
  
  network:
    session_affinity: true
  </code></pre>
  
  <p>Socket.io is used on the server in a straightforward way:</p>
  
  <pre><code>this.server = this.app.listen(this.PORT, (err: any) =&gt; {
    io = socketio().listen(this.server);
    io.on("connection", (socket) =&gt; {
      console.log('A socket connection was made!');
    });
  });
  </code></pre>
  
  <p>I am wondering how to get the websocket connection to work on App Engine? Maybe a firewall rule or other configuration change is needed?</p>