<p>Apologies in advance for such a long question, I just want to make sure I cover everything...</p>
<p>I have a react application that is supposed to connect to a socket being run in a service that I have deployed to kubernetes. The service runs and works fine. I am able to make requests without any issue but I cannot connect to the websocket running in the same service.</p>
<p>I am able to connect to the websocket when I run the service locally and use the locahost uri.</p>
<p>My express service's server.ts file looks like:</p>
<pre><code>import &quot;dotenv/config&quot;;
import * as packageJson from &quot;./package.json&quot;

import service from &quot;./lib/service&quot;;
const io = require(&quot;socket.io&quot;);

const PORT = process.env.PORT;

const server = service.listen(PORT, () =&gt; {
  console.info(`Server up and running on ${PORT}...`);
  console.info(`Environment = ${process.env.NODE_ENV}...`);
  console.info(`Service Version = ${packageJson.version}...`);
});

export const socket = io(server, {
  cors: {
    origin: process.env.ACCESS_CONTROL_ALLOW_ORIGIN,
    methods: [&quot;GET&quot;, &quot;POST&quot;]
  }
});

socket.on('connection', function(skt) {
  console.log('User Socket Connected');
  socket.on(&quot;disconnect&quot;, () =&gt; console.log(`${skt.id} User disconnected.`));
});

export default service;
</code></pre>
<p>When I run this, <code>PORT</code> is set to 8088, and access-control-allow-origin is set to <code>*</code>. And note that I'm using a rabbitmq cluster that is deployed to Kubernetes, it is the same uri for the rabbit connection when I run locally. Rabbitmq is NOT running on my local machine, so I know it's not an issue with my rabbit deployment, it has to be something I'm doing wrong in connecting to the socket.</p>
<p>When I run the service locally, I'm able to connect in the react application with the following:</p>
<pre><code>const io = require(&quot;socket.io-client&quot;);
const socket = io(&quot;ws://localhost:8088&quot;, { path: &quot;/socket.io&quot; });
</code></pre>
<p>And I see the &quot;User Socket Connected&quot; message and it all works as I expect.</p>
<p>When I deploy the service to Kubernetes though, I'm having some issues figuring out how to connect to the socket.</p>
<p>My Kubernetes Service:</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  type: ClusterIP
  ports:
    - port: 80
      targetPort: 8088
  selector:
    app: my-service
</code></pre>
<p>My deployment:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-service
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-service
  template:
    metadata:
      labels:
        app: my-service
    spec:
      containers:
        - name: project
          image: my-private-registry.com
          ports:
            - containerPort: 8088
      imagePullSecrets:
        - name: mySecret
</code></pre>
<p>And finally, my ingress:</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-service-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    cert-manager.io/cluster-issuer: letsencrypt-prod
    nginx.ingress.kubernetes.io/enable-cors: &quot;true&quot; // Just added this to see if it helped
    nginx.ingress.kubernetes.io/cors-allow-origin: &quot;*&quot; // Just added this to see if it helped
    nginx.ingress.kubernetes.io/cors-allow-methods: PUT, GET, POST, OPTIONS, DELETE, PATCH // Just added this to see if it helped
spec:
  tls:
    - hosts:
        - my.host.com
      secretName: my-service-tls
  rules:
    - host: &quot;my.host.com&quot;
      http:
        paths:
          - pathType: Prefix
            path: &quot;/project&quot;
            backend:
              service:
                name: my-service
                port:
                  number: 80
</code></pre>
<p>I can connect to the service fine and get data, post data, etc. but I cannot connect to the websocket, I get either 404 or cors errors.</p>
<p>Since the service is running on my.host.com/project, I assume that the socket is at the same uri. So I try to connect with:</p>
<pre><code>const socket = io(&quot;ws://my.host.com&quot;, { path: &quot;/project/socket.io&quot; });
</code></pre>
<p>and also using wss://</p>
<pre><code>const socket = io(&quot;wss://my.host.com&quot;, { path: &quot;/project/socket.io&quot; });
</code></pre>
<p>and I have an error being logged in the console:</p>
<pre><code>socket.on(&quot;connect_error&quot;, (err) =&gt; {
  console.log(`connect_error due to ${err.message}`);
});
</code></pre>
<p>both result in</p>
<pre><code>polling-xhr.js?d33e:198 GET https://my.host.com/project/?EIO=4&amp;transport=polling&amp;t=NjWQ8Tc 404
websocket.ts?25e3:14 connect_error due to xhr poll error
</code></pre>
<p>I have tried all of the following and none of them work:</p>
<pre><code>const socket = io(&quot;ws://my.host.com&quot;, { path: &quot;/socket.io&quot; });
const socket = io(&quot;wss://my.host.com&quot;, { path: &quot;/socket.io&quot; });
const socket = io(&quot;ws://my.host.com&quot;, { path: &quot;/project&quot; });
const socket = io(&quot;wss://my.host.com&quot;, { path: &quot;/project&quot; });
const socket = io(&quot;ws://my.host.com&quot;, { path: &quot;/&quot; });
const socket = io(&quot;wss://my.host.com&quot;, { path: &quot;/&quot; });
const socket = io(&quot;ws://my.host.com&quot;);
const socket = io(&quot;wss://my.host.com&quot;);
</code></pre>
<p>Again, this works when the service is run locally, so I must have missed something and any help would be extremely appreciated.</p>
<p>Is there a way to go on the Kubernetes pod and find where rabbit is being broadcast to?</p>

Thomas Colbert
<p>In case somebody stumbles on this in the future and wants to know how to fix it, it turns out it was a really dumb mistake on my part..</p>
<p>In:</p>
<pre><code>export const socket = io(server, {
  cors: {
    origin: process.env.ACCESS_CONTROL_ALLOW_ORIGIN,
    methods: [&quot;GET&quot;, &quot;POST&quot;]
  },
});
</code></pre>
<p>I just needed to add <code>path: &quot;/project/socket.io&quot;</code> to the socket options, which makes sense.</p>
<p>And then if anybody happens to run into the issue that followed, I was getting a 400 error on the post to the websocket polling so I set <code>transports: [ &quot;websocket&quot; ]</code> in my socket.io-client options and that seemed to fix it. The socket is now working and I can finally move on!</p>
