0

My javascript game application will not work when I use pm2 start process.yml. The game works just fine when I use npm start but not pm2 start process. The index.js file on server loads but then gives me errors such as:

WebSocket connection to 'ws://www.zeyeland.com:3000/socket.io/?EIO=3&transport=websocket&sid=gwcGq7h5vHrxBGOyAAAA' failed: WebSocket is closed before the connection is established.

and

GET http://www.zeyeland.com:3000/socket.io/?EIO=3&transport=polling&t=MEzN2OK&sid=gwcGq7h5vHrxBGOyAAAA 400 (Bad Request)

Once again my program runs smoothly when I start my server using npm start.

Here is my process.yml file

apps:
  - script: node index.js
    watch: true
    instances: 4
    exec_mode: cluster
    env:
      NODE_ENV: development
    env_production: 
      NODE_ENV: production

package.json

{
    "name": "sockets",
    "version": "0.0.1",
    "description": "web sockets demo",
    "scripts": {
        "start": "node index.js"
    },
    "dependencies": {
        "express": "^4.16.3",
        "socket.io": "^2.1.1"
    }
}

What can be the problem? Does it have something to do with my apache2 reverseProxy? I was getting a similar error earlier on when I attempted to configure my reverse proxy for nodejs app running on zeyeland:3000 to zeyeland/bomber-kids-online.

The code I added to my .bashrc file on my server to configure my pm2 clusters is

cd /var/www/html/bomber-kids && pm2 start process.yml && cd ~

When I run pm2 start process.yml, the clusters start and everything appears to be working until I access websites, and then the game gives a console error due to socket issues.

Mohammad Raheem
  • 1,131
  • 1
  • 13
  • 23
sirshakir
  • 129
  • 4
  • 13
  • I tried hard coding my script src for the socket.io. The path is :http://myNodjsApp.com/node_modules/socket.io-client/dist/socket.io.js" With this direct route to socket.io component my pm2 process.yml still doesnt not work but works fine with npm start. Are nodejs websockts compatible with reverseProxy and apache? – sirshakir Jun 02 '18 at 02:03
  • The pm2 start process configure a different execution path, Try to start pm2 using this command : pm2 start npm -- start – Mohammad Raheem Jun 02 '18 at 10:07
  • Gratitude! Thanks man, that was the trick. What wait, so i was using the wrong command? Wait does " pm2 start process.yml " do? It starts up four clusters and runs port 3000 before crashing. Using the " pm2 start npm -- start " you suggested starts up the npm fork below my clusters. Thats strange because I created this running npm fork last night while researching. I dont recall the artilce. I had no knowledge on but followed instructions. Could you explain more on what is happening with these process? Thanks again! – sirshakir Jun 02 '18 at 19:46
  • Please share logs using pm2 logs So we can have better understanding of your issue. – Muhammad Zubair Saleem Jun 03 '18 at 06:21
  • where can these files be located? In the /etc/pm files on my server? – sirshakir Jun 04 '18 at 14:13

1 Answers1

0

pm2 cluster mode doesn't work well with socket.io sticky-session. See my question here How to make sticky session works with socket.io?

You can force socket.io use websocket

Qiulang
  • 10,295
  • 11
  • 80
  • 129