3
votes

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.

2
I think you should fill an issue at github, because even when I overrided the logger etc, that first message still got displayed on screen.Alfred

2 Answers

4
votes

That statement is in /usr/local/lib/node_modules/socket.io/lib/manager.js on line 117 for socket.io 0.7.9 (if you aren't using a global npm install then it'll be in wherever you've installed your npm modules). Open the file, go to line 117 and comment it out. Then clean the cache (npm cache clean $HOME/.npm/socket.io) and restart and it should be gone.

1
votes

I received a message from tedah, a Socket.io contributor. His answer this problem was to pass options to the manager.js through the listener method as such…

options.logger = {
   info: self.emit.bind(self, 'info'),
   error: self.emit.bind(self, 'error'),
   warn: self.emit.bind(self, 'error'),
   debug: new Function()
 };

var io = socketIo.listen(port, options);

Where options.logger.info etc are bound to the container's (self) emit methods and options.logger.debug is ignored.