Replace pm2 with @socket.io/pm2

I need to replace pm2 with @socket.io/pm2.
I followed the instruction at Usage with PM2 | Socket.IO and removed pm2 and installed @socket.io/pm2 globally. But then my strapi installation is not starting anymore.

Reverting to pm2 brought back strapi. But I use the strapi-plugin-io which is not working with pm2. How can I replace pm2 and get socket.io working?

I disabled http polling on client side, to use the standard pm2.
Had to add following to strapi site nginx config to make websockets work:

location /socket.io/ {
    proxy_pass http://localhost:8423;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_set_header Host $host;
  }

But messages are not send to all clients.

This nginx settings work better:

location /socket.io/ {
    proxy_pass http://localhost:8423;
    proxy_redirect off;
    proxy_pass_request_headers on;
    proxy_set_header Host $http_host;
    
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection Upgrade;
  }

(source: java - nginx as a proxy for Websocket to send text messages - Stack Overflow)

But still have issues. Could be a problem on client side. I keep on investigating.

I am still struggling with that. The socket exists, but the client does not get all messages from the strapi endpoint.
In development enviroment it works perfectly, so I guess it is the nginx proxy. I tried differnet configs, but no success. There is no entry in the error log of the strapi nginx error logfile.
How can I debug this? What could be the problem? My last strapi nginx config:

location /socket.io/ {
      proxy_pass http://localhost:8423;
      proxy_pass_header Server;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_pass_header Server;

      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-Forwarded-Host $host;
      proxy_set_header X-Forwarded-Port $server_port;
       
      proxy_redirect off;
      proxy_pass_request_headers on;
    
  }

It is the cluster mode in pm2.
I turned off the ngnix proxy and switched the socket.io client to transport “polling”. The problems remained. However, the browser console now showed “unknown Session ID” errors. This also got me tracking down the cluster mode in pm2. I am using a cloud server with 2 CPUs. Now when a request started from the first CPU but the response is sent from the second CPU, the socket.io message no longer arrives at the client because it is a different session. Turning off cluster mode in pm2 and limiting it to one CPU solves the problem.

instances : "1", // change the value to "1" if your server has only 1 CPU
// exec_mode : "cluster_mode", // remove this line if your server has only 1 CPU

Now it works with the nginx proxy settings documented at socket.io:

location /socket.io/ {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $host;

      proxy_pass http://localhost:3000;

      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
}

I do have to make the app stateless: PM2 - Stateless Application | Best Practices | PM2 Documentation or get @socket.io/pm2 to work. But for now this setup is ok. No heavy traffic for this app.

2 Likes