简体   繁体   中英

Why can't I run node.js & socket.io in a screen or with Forever?

I've got some annoying problem and that is I have trouble in running nodejs in a screen. Because as fast as I leave the screen and no sockets are connected the next whom is to connect will be faced by an error message until the screen is opened again. As soon as the screen is open with screen -R node (for example) it will start accepting connections again.

However notice that if the screen is closed (running in the background) and someone already has a socket open, it will continue to answer new connections.

When I try to start the application again, with the command node app I get the following message:

module.js:340
    throw err;
          ^
Error: Cannot find module '(unreachable)/square_defense/app'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.runMain (module.js:492:10)
    at process.startup.processNextTick.process._tickCallback (node.js:244:9)

Server side code :

var app = require('express')()
  , server = require('http').createServer(app)
  , io = require('socket.io').listen(server);

server.listen(3000);

app.get('/', function (req, res) {
  console.log(__dirname);
  res.sendfile(__dirname + '/index.html');
});

io.sockets.on('connection', function (socket) {
    socket.on('user-message', function (data) {
        console.log(data);
        sendMessage.call(socket, data.message);
    });
});
var sendMessage = function(message) {
      this.emit('server-message', {message: message});
      this.broadcast.emit('server-message', {message: message});
}

I have tried a lot of things like npm install app in directory .. from the app.js. Can't understand why it dont want to work. I really just want a server which I can use for testing purposes that do not crash.

EDIT: I think this has something to do with an encrypted home folder?

I think the unreachable folder is where the problem resides. Error: Cannot find module ' (unreachable) /square_defense/app'. But I don't know why this is happening. Right now I am running the application with Forever and as soon as I have an ssh-session on it will work. But as soon as I exit the connection and reload the page. It will generate an error.

As you said, your home directory is encrypted, and according to the document from Ubuntu :

This directory is automatically mounted on login, and unmounted on logout.

So your guess is correct, this is exact what's causing the error. Running a detached screen session won't be considered by the OS as the user are still logged in*. The time all shell sessions of the user ends, the encrypted home directory got unmounted.


Solution: You can move your app to a directory other than $HOME , then I think either GNU Screen or forever will do the job without errors.†


* You can test this by your self: login, start a screen session, run something like vim , detach; use command w to see who is logged in and what they are doing

† I think if you write your own upstart scripts for this, it is still crucial that the app files are not placed in auto-encrypted home directory.

Since you already are on an Ubuntu box, i recommend using Upstart and monit as described in this post: http://howtonode.org/deploying-node-upstart-monit

The only thing i added into the mix was a frontend proxy-server (HAProxy in my case, but nginx works as well) in order to serve my application on port 80.

I deployed a socket.io/express app using this technology stack on Amazon EC2 myself about a month ago, with amazing results!

EDIT:

Here are some resources i collected over time concerning a proper production setup using node.js/nginx/monit/git:

I really recommend the first one, you might have to skip some parts (like npm module installation or system setup), but the parts about nginx/git and supervisor are worth a read.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM