I have a Node script that uses Socket.io to broadcast messages to connected clients. I want to run it as a background process with an Upstart script. I can start the daemon with sudo start my_server
and the process will start and run. However, the server is stopped immediately by Socket.io's standard output 'info - (Socket.io started)'.
This is executed, before the Socket.io configuration is read. Socket.io documentation shows that I can pass a 'log' option to the listen method to keep sys.log from sending output to stdout. But I can't seem to keep that initial 'info - (Socket.io started)' from being output. I even tried to comment out that call to logger in the module but it is cached.
Has anyone successfully created a node/socket.io daemon or suppressed socket.io's default output? Below is the code for my Upstart script and my node.js.
upstart script
description "node.js server"
author "some@email.com"
start on started mountall
stop on shutdown
respawn
respawn limit 99 5
script
export HOME="/username"
exec sudo -u username sh -c "/usr/local/bin/node path/to/server.js >> /var/log/node.log 2?&1"
end script
server.js using socket.io
var http = require('http'),
url = require('url'),
fs = require('fs'),
//create server and attach socket io
server = http.createServer(function(req, res){
res.writeHead(200,{ 'Content-Type': 'text/html' });
res.end('<h1>Hello Socket Lover!</h1>');
}),
io = require('socket.io').listen(server, function(message){});
//configure socket.io logging so it is minimal
io.configure(function(){
io.set('log level', 1);
io.set('transports', [
'websocket',
'flashsocket',
'htmlfile',
'xhr-polling',
'jsonp-polling'
]);
});
//instruct our server to listen to port 8080
server.listen(8080);
io.sockets.on('connection', function (socket) {
socket.emit('message', { eventType: "connected" });
});
I appreciate any answers or solution.