<p>I'm trying to get a simple websocket connection working on my server running in a Kubernetes cluster.</p>

<p>Websocket connections are able to establish on my local test machine but I can't connect my client side to the server after I deploy to GKE with nginx-ingress.</p>

<p>ingress yaml file:</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-service
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.org/websocket-services : "socket-cluster-ip-service"
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/websocket-services : "socket-cluster-ip-service"
    nginx.ingress.kubernetes.io/proxy-send-timeout: "1800"
    nginx.ingress.kubernetes.io/proxy-read-timeout: "1800"
spec:
  rules:
    - host: my-socket.com
      http:
        paths:
          - path: /
            backend:
              serviceName: socket-cluster-ip-service
              servicePort: 3000
    - host: www.my-socket.com
      http:
        paths:
          - path: /
            backend:
              serviceName: socket-cluster-ip-service
              servicePort: 3000
</code></pre>

<p>service yaml file:</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: socket-cluster-ip-service
spec:
  type: ClusterIP
  selector:
    component: socket
  ports:
  - port: 3000
    targetPort: 3000
</code></pre>

<p>deployment yaml file:</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: socket-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: socket
  template:
    metadata:
      labels:
        component: socket
    spec:
      containers:
      - name: socket
        image: gcr.io/my-socket/socket
        ports:
        - containerPort: 3000
</code></pre>

<p>express server code:</p>

<pre><code>...

var server = http.createServer(app);
const WebSocket = require('ws');

const wss = new WebSocket.Server({ server: server });

wss.on('connection', function connection(ws) {
  console.log("new connection", ws)
  ws.on('message', function incoming(message) {
    console.log('received: %s', message);
  });

  ws.send('something');
});

...
</code></pre>

<p>client side code:</p>

<pre><code>var ws = new WebSocket('ws://www.my-socket.com/');

ws.onopen = () =&gt; {
  // connection opened
  ws.send('something'); // send a message
};

ws.onmessage = (e) =&gt; {
  // a message was received
  console.log(e.data);
};

ws.onerror = (e) =&gt; {
  // an error occurred
  console.log(e.message);
};

ws.onclose = (e) =&gt; {
  // connection closed
  console.log(e.code, e.reason);
};
</code></pre>

<p>I keep getting the error message "Invalid Sec-WebSocket-Accept response" from the client side. But I think it's just the fact that the client could not establish websocket connection with the server.</p>

<p>The server side library I used is <a href="https://github.com/websockets/ws" rel="noreferrer">ws</a></p>

<p>I've added the following annotations to my ingress yaml file as suggested by <a href="https://stackoverflow.com/questions/53770981/using-socket-io-on-gke-with-nginx-ingress">other post</a> and <a href="https://kubernetes.github.io/ingress-nginx/user-guide/miscellaneous/#websockets" rel="noreferrer">official ingress-nginx documentation</a> but it's still not working:</p>

<pre><code>nginx.org/websocket-services : "socket-cluster-ip-service"
nginx.ingress.kubernetes.io/websocket-services : "socket-cluster-ip-service"
nginx.ingress.kubernetes.io/proxy-send-timeout: "1800"
nginx.ingress.kubernetes.io/proxy-read-timeout: "1800"
</code></pre>

<p>For me it works like a charm!<br />
I'm using
<strong>kong-docker-kubernetes-ingress-controller.bintray.io/kong-ingress-controller:0.9.0</strong>
on my private kubernetes cluster.<br />
Thus, your issue might be related to GCE.</p>
<p>Please also check your TLS configuration.<br />
In your post you were mentioning <code>ws://...</code> rather than <code>wss://</code>, which indicates you don't even use TLS. This might be rejected by your browser (e.g. Chrome).</p>
