title: Error during WebSocket handshake: Unexpected response code: 502 on Minikube with Ingress
body: <p>Trying to create a simple websocket server on minikube with ingress.
  While connecting with the ws://192.168.99.119/ getting the below error.</p>
  
  <p><strong>WebSocket connection to 'ws://192.168.99.119/' failed: Error during WebSocket handshake: Unexpected response code: 502</strong></p>
  
  <p>Please help me to fix this issue. Below provided the details of the implementation.</p>
  
  <blockquote>
    <p>Websocket Server implementation</p>
  </blockquote>
  
  <pre><code>import * as express from 'express';
  import * as http from 'http';
  import * as WebSocket from 'ws';
  
  interface ExtWebSocket extends WebSocket {
      id: string; // your custom property
      isAlive: boolean
  }
  const PORT = parseInt(process.env.NODE_PORT) || 8010;
  
  const app = express();
  
  //initialize a simple http server
  const server = http.createServer(app);
  
  //initialize the WebSocket server instance
  const wss = new WebSocket.Server({ server });
  
  wss.on('connection', (ws: ExtWebSocket) =&gt; {
  
      //connection is up, let's add a simple simple event
      ws.on('message', (message: string) =&gt; {
  
          //log the received message and send it back to the client
          console.log('received: %s', message);
  
          const broadcastRegex = /^broadcast\:/;
  
          ws.isAlive = true;
  
          ws.on('pong', () =&gt; {
              ws.isAlive = true;
          });
  
          if (broadcastRegex.test(message)) {
              message = message.replace(broadcastRegex, '');
  
              //send back the message to the other clients
              wss.clients
                  .forEach(client =&gt; {
                      if (client != ws) {
                          client.send(`Hello, broadcast message -&gt; ${message}`);
                      }
                  });
  
          } else {
              ws.send(`Hello, you sent -&gt; ${message}`);
          }
      });
  
      //send immediatly a feedback to the incoming connection
      ws.send('Hi there, I am a WebSocket server');
  });
  
  setInterval(() =&gt; {
      wss.clients.forEach((ws: ExtWebSocket) =&gt; {
  
          if (!ws.isAlive) return ws.terminate();
  
          ws.isAlive = false;
          ws.ping(null, false);
      });
  }, 10000);
  
  //start our server
  server.listen(PORT, () =&gt; {
      console.log(`Server started on port ${JSON.stringify(server.address())}`);
  });
  </code></pre>
  
  <blockquote>
    <p>Dockerfile</p>
  </blockquote>
  
  <pre><code>
  FROM node:alpine
  WORKDIR /app
  COPY package*.json /app/
  RUN npm install
  
  COPY ./ /app/
  RUN npm run build
  
  CMD ["node","./dist/server.js"]
  
  EXPOSE 8010
  
  </code></pre>
  
  <blockquote>
    <p>Websocket service yaml</p>
  </blockquote>
  
  <pre><code>apiVersion: apps/v1
  kind: Deployment
  metadata:
    name: socketserver
  spec:
    replicas: 1
    selector:
      matchLabels:
        app: socketserver
    template:
      metadata:
        labels:
          app: socketserver
      spec:
        containers:
        - name: socketserver
          image: kireeti123/socketserver:1.0.2
          resources:
            limits:
              memory: "128Mi"
              cpu: "500m"
          ports:
          - containerPort: 8010
            name: wsport
  ---
  apiVersion: v1
  kind: Service
  metadata:
    name: socketserver-svc
  spec:
    selector:
      app: socketserver
    ports:
    - port: 8010
      targetPort: 80
  ---
  
  </code></pre>
  
  <blockquote>
    <p>Ingress.yaml</p>
  </blockquote>
  
  <pre><code>apiVersion: extensions/v1beta1
  kind: Ingress
  metadata:
    name: demo-ingress
    annotations:
      kubernetes.io/ingress.class: nginx
      nginx.ingress.kubernetes.io/websocket-services: socketserver-svc
      nginx.org/websocket-services: socketserver-svc
  spec:
    rules:
    - http:
        paths:
        - path: /
          backend:
            serviceName: socketserver-svc
            servicePort: 80
  </code></pre>