Search code examples
node.jsangularelectronnode-modulesforever-monitor

How to use forever-monitor with Electron-Angular project?


I am using Angular 2 with Electron and want to keep running a process in background to show notifications. I am using forever-monitor for that, it works only in development mode, but when I package my app using electron-packager, this stops working. My code looks like that:

main.ts

exports.runBackgroundProcess = () =>  {

// Run a background process forever
var forever = require('forever-monitor');
var child = new(forever.Monitor)('src/assets/notification-process.js', 
{
  env: {ELECTRON_RUN_AS_NODE: 1},
  options: []
});

child.start();
}

I wrote a function in main.ts that will run background process when called from angular component. Code in notification-process.js is following:

notification-process.js

notifier = require('node-notifier')

notifierFun = (msg) =>  {
 notifier.notify({
 title: 'Notify Me',
 message: msg,
 wait: true
 });
}

var CronJob = require('cron').CronJob;

new CronJob('* * * * * *', function() {
  notifierFun("Message from notification process");
});

Finally I am calling the function from app.component.ts

let main_js  = this.electronService.remote.require("./main.js");
main_js.runBackgroundProcess();

Solution

  • That's how it worked:

    1- Moved notification-process.js file from assets folder to main directory.

    2- Changed file path in main.js:

    var child = new (forever.Monitor)(path.join(__dirname, 'notification-process.js')...
    

    Without using join, it doesn't work after packaging the app.