Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Socketio usage with PM2 cluster mode #5054

Open
sridhard opened this issue Jun 22, 2024 · 0 comments
Open

Socketio usage with PM2 cluster mode #5054

sridhard opened this issue Jun 22, 2024 · 0 comments

Comments

@sridhard
Copy link

sridhard commented Jun 22, 2024

Describe the bug
I am using socketio with PM2 and cluster mode.
I am using @socket.io/pm2. Also used socket.io/cluster-adapter and socket.io/sticky s per the docs.

In client side I changed transport to polling (Removed websocket).
With websocket in client side everything is working properly.
When using polling, client connects with server and disconnects.

To Reproduce

Please fill the following code example:

Versions:
"@socket.io/cluster-adapter": "^0.2.2",
"@socket.io/sticky": "^1.0.4",
"socket.io": "^4.7.5",

Server

const fp = require('fastify-plugin')
const { Server } = require("socket.io");
const { createAdapter } = require("@socket.io/cluster-adapter");
const { setupWorker } = require("@socket.io/sticky");

async function socketio(fastify, opts = {}) {
  console.log("Creating sockio");

  const io = new Server(fastify.server, opts);
  io.adapter(createAdapter());
  setupWorker(io);

  fastify.decorate("io", io);
  fastify.addHook("onClose", (fastify, done) => {
    fastify.io.close();
    done();
  });  
}

module.exports = fp(socketio);
module.exports = (fastify) => {
  fastify.ready().then(() => {
    fastify.io.on("connection", (socket) => {

      console.log(`New client connected via: ${socket.conn.transport.name}`);

      socket.conn.on("upgrade", () => {
        console.log(`Transport changed to ${socket.conn.transport.name}`);
      });

      socket.on("message", (msg) => {
        console.log("Message received over connection type : ", socket.conn.transport.name);
        socket.emit("reply", "Message received loud and clear!");
      });

      socket.on("disconnect", () => {
        console.log("Connection disconnected");
      });
    });
  });
};

Client

https://cdn.jsdelivr.net/npm/socket.io-client/dist/socket.io.js

    // Connect to the Socket.IO server
    const socket = io('https://chat.xyz.com', {
      transports:[ 'polling']
    }); // Connect to the local server

    // Function to append messages to the message div
    function appendMessage(message) {
      const messageDiv = document.getElementById('messages');
      const newMessage = document.createElement('div');
      newMessage.textContent = message;
      messageDiv.appendChild(newMessage);
    }

    // Listen for connection event
    socket.on('connect', () => {
      console.log('Connected to server');
      appendMessage('Connected to server');
    });

    socket.on('disconnect', () => {
      console.log('Disconnected from server');
      appendMessage('Disconnected from server');
    });

    // Listen for reply event
    socket.on('reply', (msg) => {
      console.log('Received reply:', msg);
      appendMessage(`Reply: ${msg}`);
    });

    // Listen for error event
    socket.on('error', (error) => {
      console.error('Error received from server:', error);
      appendMessage(`Error: ${error}`);
    });

    // Handle the send message button click
    document.getElementById('sendMessageButton').addEventListener('click', () => {
      const message = 'Hello from client';
      console.log('Sending message:', message);
      appendMessage(`Sent: ${message}`);
      socket.emit('message', message);
    });

Expected behavior
In polling mode pm2 sticky session should work and client should not get disconnect.

Platform:

  • Device: mac chrome browser
  • OS: Mac sonoma

Additional context
With websockets it is working. I am testing with polling for fallback support

@sridhard sridhard added the to triage Waiting to be triaged by a member of the team label Jun 22, 2024
@darrachequesne darrachequesne added needs investigation and removed to triage Waiting to be triaged by a member of the team labels Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants