In a recent project we are using websockets to respond in Apex to certain database events.
The websocket server is based upon node.js. One of the tasks was to setup this server as an service under a normal Windows Server 2003 (yes I know it is old) infrastructure. This post describes the steps how to setup such a service and also includes some monitoring information.
The prerequisties are that you already need to have node installed on your machine.
To start the websocket server we would usually call
node synwsserver.js
This starts the server. All the output (console.log) will be written to the terminal.
But we don’t want to run it manually each time. Instead we would like to setup it as a windows service. So here is how to achieve that. The same logic can be applied for any node module, not just for websockets.
1) Load node-windows
The node package that I used is node-windows. It is very lightweight and did not have dependencies to node-gyp which often gives trouble.
The command to install it is:
npm install node-windows
The author recommends to install it using the global flag -g. You might want to consider it. I did not encounter issues without the global flag.
2) Install the service
In the node “shell” run the following commands.
The script name is the same script that would be called directly from node.
File: installServiceApexWS.js
var Service = require('node-windows').Service; // Create a new service object var svc = new Service({ name:'syn-apex-websocket', description: 'The websocket server for the APEX project.', script: 'D:\\tomcat\\nodejs\\synwsserver.js' }); // Listen for the 'install' event, which indicates the // process is available as a service. svc.on('install',function(){ svc.start(); }); // install the service svc.install();
The name and the description can then be seen in the windows service tool.
Result
And we are able to start and stop the service.
Problem solved!
Ok there is a tiny bit more. We want to be able to uninstall it as well. And we need to think about the messages that were previously written to the console.
Run in Batch
The sequence of javascript commands can also be put into a .BAT file. I choose to separate the batch call from the js code, so there are two files now.
File: installServiceApexWS.bat
echo "Installing service..." start "install service - Syntegris APEX websocket server" node installServiceApexWS.js echo "Service installiert." exit;
3) Uninstall the service
The logic to deinstall the service is very similar to installing it.
File: uninstallServiceApexWS.js
var Service = require('node-windows').Service; // Create a new service object var svc = new Service({ name:'syn-apex-websocket', description: 'The websocket server for the APEX project.', script: 'D:\\tomcat\\nodejs\\synwsserver.js' }); // Listen for the 'uninstall' event so we know when it is done. svc.on('uninstall',function(){ console.log('Uninstall complete.'); console.log('The service exists: ',svc.exists); }); // Uninstall the service. svc.uninstall();
File: uninstallServiceApexWS.bat
echo "Uninstalling service..." start "uninstall service - Syntegris APEX websocket server" node uninstallServiceApexWS.js echo "Service deinstalliert." exit;
4) Add event logging
Node-windows comes with same basic windows event logging. That means certain type of actions can be written into the default windows eventlog.
An example
/* add logging for Windows events */ var EventLogger = require('node-windows').EventLogger; var log = new EventLogger('syn-apex-websocket'); ... /* Start listening on the configured port and write to standard output*/ server.listen(config.port, function() { console.log((new Date()) + ' Server is listening on port ' + config.port); log.info('Server is listening on port ' + config.port); });
An this is how the result looks like in the Event Viewer (search for Event on your Windows Server) to find the tool.
Where does config.port come into the picture?
Hello Tobin,
That’s a good question. I had to double check it myself, since the code is running stable for quite some time now.
The post just describes how to make a node websocket server running as a service that can be seen in the windows service panel.
The configuration of the websocket server itself is hidden inside the synwsserver.js file.
The start of the file looks like this
Regards
Sven
from where you are getting this script file
D:\\tomcat\\nodejs\\synwsserver.js
Thats is my own script for running a node.js webserver. If you google for “node websocket server” you will find hundreds of examples.
My blog post only describes how to add such a node.js websocket server to the normal windows service topology.
thanks for great tutorial.
i try to make service for storing data from pubnub cloud to mysql, and with this tutorial absolutely need the web socket.thanks alot,
Hello svenweller.
Thanks for this great article. I tried the exact steps you have mentioned above. The service does gets installed and it starts. But after 30 sec the service stops automatically. This is the behavior even if we manually start the service.
Any hints on this behavior ?
Once the script is running as a service, how do you view the console output?
The event viewer is a windows tool, that will show the logs.
No–
var log = new EventLogger(“service”);
causes the script to crash with the error code,
“ReferenceError: EventLogger is not defined”
Strange. You need to load the Eventlogger function via a require call to node-windows first. Did this work?
var EventLogger = require(‘node-windows’).EventLogger;
var log = new EventLogger(‘syn-apex-websocket’);
[…] https://svenweller.wordpress.com/2016/07/01/howto-node-windows-service/ […]
hello,
I have a doubt regarding node-windows package, I have put question on stackoverflow, cam you please help me with this?
https://stackoverflow.com/questions/61114221/how-to-call-function-from-nodejs-running-as-windows-service
I believe the easiest way to run a nodejs app as a Windows service is by using nssm to create the service. Just a couple of commands and you’re done. Here’s a short version of the guide: https://www.helpmegeek.com/run-nodejs-application-as-windows-service/