Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.io startup output kills Upstart script for node.js daemon

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      "[email protected]"

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.

like image 817
user631630 Avatar asked Nov 27 '25 09:11

user631630


2 Answers

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.

like image 199
Femi Avatar answered Nov 29 '25 02:11

Femi


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.

like image 35
user631630 Avatar answered Nov 29 '25 03:11

user631630



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!