<p>i have a kubernetes environment i am trying to publish my socket.io nodejs app to it for scaling.</p>
<p>It only emits the message to clients connected to same server, other clients are not getting any message at all.</p>
<p>Using:</p>
<p>socket.io@4.1.0</p>
<p>sticky@1.0.1</p>
<p>cluster-adapter@0.1.0</p>
<p>I was following this documentation:</p>
<p>Here is my clustered-socket.js:</p>
<pre><code>const cluster = require(&quot;cluster&quot;);
const http = require(&quot;http&quot;);
const { Server } = require(&quot;socket.io&quot;);
const numCPUs = require(&quot;os&quot;).cpus().length;
const { setupMaster, setupWorker } = require(&quot;@socket.io/sticky&quot;);
const { createAdapter, setupPrimary } = require(&quot;@socket.io/cluster-adapter&quot;);

if (cluster.isMaster) {
    console.log(`Master ${process.pid} is running`);

    const httpServer = http.createServer();

    setupMaster(httpServer, {
        loadBalancingMethod: &quot;least-connection&quot;,
    });

    setupPrimary();

    cluster.setupMaster({
        serialization: &quot;advanced&quot;,
    });

    httpServer.listen(80);

    console.log(`Starting ${numCPUs} workers...`);

    for (let i = 0; i &lt; numCPUs; i++) {
        var worker = cluster.fork();
        console.log(`Worker ${worker.process.pid} started.`);
    }

    console.log(`Started ${numCPUs} workers.`);

    cluster.on(&quot;exit&quot;, (worker) =&gt; {
        console.log(`Worker ${worker.process.pid} died`);
        cluster.fork();
    });
} else {
    console.log(`Worker ${process.pid} started`);

    const httpServer = http.createServer();
    const io = new Server(httpServer, {
        transports: [&quot;websocket&quot;],
        cors: {
            origin: &quot;*&quot;,
            methods: [&quot;GET&quot;, &quot;POST&quot;]
        }
    });

    io.adapter(createAdapter());
    setupWorker(io);

    io.on('connection', (socket) =&gt; {
        socket.emit(&quot;MESSAGE&quot;, &quot;Welcome to Stream Socket.&quot;);

        socket.on('disconnect', () =&gt; { });

        socket.on('SUBSCRIBE', (msg) =&gt; {
            try {
                console.log(msg);
                
                var obj = JSON.parse(msg);
                socket.join(obj.requestedStream);

                if (obj.requestedStream.startsWith(&quot;OLD_MESSAGES&quot;)) {
                    // This here is only emitting to clients connected to same server.
                    io.to(&quot;OLD_MESSAGES&quot;).emit(&quot;___OLD MESSAGES HERE____&quot;);
                }

            } catch (e) { console.log(e); }
        });

        socket.on('UNSUBSCRIBE', (msg) =&gt; {
            var obj = JSON.parse(msg);
            socket.leave(obj.requestedStream);
        });
    });
}
</code></pre>
<p>I've enabled sticky-session on kubernetes with nginx, ingress:</p>
<pre><code>nginx.ingress.kubernetes.io/affinity: &quot;cookie&quot;
nginx.ingress.kubernetes.io/affinity-mode: &quot;persistent&quot;
nginx.ingress.kubernetes.io/affinity-canary-behavior: &quot;sticky&quot;
nginx.ingress.kubernetes.io/session-cookie-name: &quot;route&quot;
nginx.ingress.kubernetes.io/session-cookie-hash: &quot;sha1&quot;
nginx.ingress.kubernetes.io/session-cookie-expires: &quot;10800&quot;
nginx.ingress.kubernetes.io/session-cookie-max-age: &quot;10800&quot;
nginx.ingress.kubernetes.io/proxy-connect-timeout: &quot;3600&quot; 
nginx.ingress.kubernetes.io/proxy-send-timeout: &quot;3600&quot;
nginx.ingress.kubernetes.io/proxy-read-timeout: &quot;1800&quot;
nginx.ingress.kubernetes.io/send-timeout: &quot;3600&quot;
nginx.ingress.kubernetes.io/proxy-ssl-server-name: &quot;on&quot;
</code></pre>
<p>Here is how i am connecting to socket:</p>
<pre><code>    var server = document.getElementById('address').value;

    socket = io(server, {
        transports: [&quot;websocket&quot;],
        'reconnection': false,
        
    });

    socket.on('MESSAGE', (msg) =&gt; {
        logResponse(msg);
    });

    socket.on('MESSAGE', (msg) =&gt; {
        logResponse(msg);
    });
</code></pre>
<p>And finally my dockerfile:</p>
<pre><code>FROM node:14
EXPOSE 80
EXPOSE 443

WORKDIR /usr/src/app
COPY package*.json ./
RUN apt-get update &amp;&amp; \
    apt-get install -y software-properties-common &amp;&amp; \
    rm -rf /var/lib/apt/lists/*
RUN sed -i &quot;/^# deb.*multiverse/ s/^# //&quot; /etc/apt/sources.list
RUN sed -i &quot;/^# deb.*universe/ s/^# //&quot; /etc/apt/sources.list
RUN npm install
RUN npm ci --only=production
COPY . .
CMD [&quot;node&quot;, &quot;clustered-socket.js&quot;]
</code></pre>

Orhan Kalayci
<p>Use an adapter that uses a database connected to all nodes like Redis, Mongodb or Postgres adapters.</p>
