Sorry if this question is to 'newbie' but I can't see how to do it / how it works.
1/ I am creating an API using node.js, API serving some front apps
2/ To get it robust I use pm2 (great product), having nginx as proxy, fine
3/ But how could I handle multiple instance of my node.js api ? I see the 'cluter mode', fine, but what's happening while my node.js is listening on a single port (8080 for instance) ? If I configure 4 process / instances, they just can't all listen on the same port ah the same time ? ... so : do pm2 dispatch the calls between the instances (so acting as a kind of proxy indeed).
Any clue will be appreciated, how does it work ?
Greg
Starting from PM2 3.2, we changed the networking connection by using a direct Websocket connection to our server on the port 443, so you only need OUTBOUND on port 443 TCP open.
PM2 is an open-source distributed multithreaded programming environment designed to support efficiently distributed programs with a highly irregular behavior (e.g. branch and bound search, computation on sparse matrices, etc.)
config. js you specify one entry object for each server instance you want to launch through PM2. The point is that you can also specify environment vars for the different processes and that way you can setup $PORT for all the processes. The following is an example config for three different processes.
Node in cluster mode creates one master and then spawns worker process which share TCP connection so basically load is distributed among workers.
Here is a beautiful article which elaborates both pm2 and node cluster -
https://getstream.io/blog/running-pm2-node-js-in-production-environments/
Hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With