Running Electron application as Windows service

In Windows or Linux, the application can be operated in the background, which is similar in concept of UNIX daemons. In this section, let's look at how we can run an Electron application or a Node.js server as a Windows server or UNIX daemons. There are some CLI tools already available in Node.js world, such as pm2 or forever, which gives the same functionality for node servers. These programs run the node server as background service in Windows or daemons in Linux.

For each platform, we need to write separate code to run the application in the background as service. To create a Windows service, the same node-windows library can be used. Let's create a small Windows service that runs in the background on Windows platform.  Background services can be invoked using CLI tools, such as pm2, forever, winser, or may be some other similar Node.js modules. In this section, let's look at how we can start a Node.js server as Windows service and integrate it into the Electron application. In Windows machine, a running service can be monitored in Windows service manager application. The following example is an Electron application that can create a web server and run it as a Windows service. You can think of this application as a small web server manager that can manage a running web server as Windows service.

Create a simple Node.js server with the following code; you need to install the express js as npm dependency into your application:

npm install --save express

Add the code into server.js as follows:

const express = require('express');
const app = express();

app.get('/', (req, res) => {
res.send('<h1>Server is up and running on port 8080</h1>');
});

app.get('*', (req, res) => {
res.send('<p>Server is running on port 8080</p>');
});

app.listen(8080, () => {
console.log('The server is running in port 8080');
});

This is a simple express server with a single route defined. There is no more complicated code in this server implementation. You can test this server using the following command:

node server.js

Let's create an interface to start this server as Windows server and Linux daemon. Add the following code into the main.js--that is your main process; define a menu with following code that starts and stop the Node.js server as a Windows service:

'use strict';
const { app, BrowserWindow, Tray, Menu } = require('electron');
const { Service } = require('node-windows');
const path = require('path');

let appShell;
let appUrl = 'file://' + __dirname + '/index.html';
let service;

function createElectronShell() {
appShell = new BrowserWindow({ width: 800, height: 600 });
appShell.loadURL(appUrl);
appShell.on('closed', () => { appShell = null; });

const template = [
{
label: 'Service',
submenu: [
{
label: 'Start service',
click() {
createService();
}
},
{
label: 'Stop service',
click() {
stopService();
}
}
]
}
];
const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu);
}

app.on('ready', createElectronShell);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin')
app.quit();
stopService();
});

app.on('activate', () => {
if (appShell == null)
createElectronShell();
});

function createService() {
if (!service) {
service = new Service({
name: 'Node JS Server',
description: 'A simple node js server',
script: path.join(__dirname, 'server.js'),
env: {
name: 'HOME',
value: process.env['USERPROFILE']
}
});
service.on('install', () => {
service.start();
});
service.install();
}
else {
service.start();
}
}

function stopService() {
if (service) {
service.stop();
}
}

This will work only on a Windows machine. Once you click to start the service menu, the Windows service will be started and can be monitored using Windows service manager window: 

If you have worked with PHPMyAdmin in Windows, you would have noted that the service can be started from the Windows tray. In a similar way, an Electron application can be used to invoke a node server or node application from your Windows tray icon or from an Electron window.

The node-windows library can also be used to list down and manage the currently running processes on a Windows machine. A simple example of managing currently running operating system tasks is as follows in an Electron application:

<html>
<head>
<script type="text/javascript">
const wincmd = require('node-windows');

window.onload = function () {
wincmd.list((svc) => {
let html = '';
svc.forEach(task => {
html += '<tr><td>' + task.ImageName + '</td><td><button onclick="killProcess(' + task.PID + ')">Kill Process</button></td></tr>';
});
document.getElementById('app').innerHTML = '<table>' + html + '</table>';
});
};

function killProcess(pid) {
wincmd.kill(pid, function () {
console.log('Process Killed');
});
}
</script>
</head>

<body>
<div id="app">
</div>
</body>

</html>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.248.252