Search code examples
node.jsraspberry-pipm2

Node JS on a Pi Zero multiple processes


I am using Node on a pi zero running raspbian stretch lite.

I basically want to have two processes running, one process is to keep a connection open to the server to push and receive messages like offline status and the other is to scan for iBeacons.

Would it be best practice to create one project and have two JSfiles and start them separately using something like PM2? Or would it be better to have two completely separate projects? The one process kind of relies on the other as I will be updating the beacons to scan for via the open connection.


Solution

  • If you're running the applications as two separate processes, PM2 allows you to control the startup of the two simultaneously through a JavaScript object, JSON, or YAML configuration file.

    For instance, running both a worker.js and api.js script separately:

    module.exports = {
      apps : [{
        name        : "worker",
        script      : "./worker.js",
        watch       : true
      }, {
        name       : "api-app",
        script     : "./api.js",
        instances  : 4,
        exec_mode  : "cluster"
      }]
    }
    

    In that sense you can keep them as one single project, and then start them up together using a single configuration file with PM2.

    If the complexity of your applications gets to the point where it might be better to split them up, you can do that as well later on.