1

I have set a node app up on my server and i can ssh into my directory and run

node server.js

great server up and running fine but as soon as i close my ssh window and logout node server stops and i get io.socket no defined etc etc.

here is my server.js

var port = process.env.PORT || 1337;

var io = require('socket.io').listen(port);

var archiveMessages = {};
var channels = ['foo channel', 'bar channel'];

var chat = io.of('/chat');
chat.on('connection', function(socket){
  console.log('connected: %s', socket.id);

  // push available channel list
  socket.emit('available_channel', channels);

  socket.on('join', function(value){
    console.log('%s joined channel: %s', socket.id, value.channelId);

    socket.join(value.channelId);
    socket.set('channel_id', value.channelId, function(){
      var messages = archiveMessages[value.channelId] || [];
      socket.emit('joined', messages);
      socket.broadcast.to(value.channelId).emit('user:join', {
        id: socket.id
      });
    });
  });

  socket.on('post', function(message){
    socket.get('channel_id', function(err, channelId){
      console.log('%s says<%s channel>: %s', socket.id, channelId, message);

      if(!(channelId in archiveMessages)){
        archiveMessages[channelId] = [];
      }
      archiveMessages[channelId].push(message);

      socket.emit('posted', {
        message: message
      });
      socket.broadcast.to(channelId).emit('user:message', {
        id: socket.id,
        message: message
      });
    });
  });

  socket.on('disconnect', function(){
    console.log('%s disconnected', socket.id);
    socket.get('channel_id', function(channelId){
      socket.leave(channelId);
      socket.broadcast.to(channelId).emit('user:leave', {
        id: socket.id
      });
    });
  });
});

var image = io.of('/image');
image.on('connection', function(socket){

  socket.on('upload', function(param){
    console.log('upload base64 size: %d', param.base64.length);

    if(param.download){
      socket.emit('download', {
        base64: 's' + new Array(65534).join('0') + 'e'
      });
    }
  });
});

How can i keep this server.js running constantly.

Thanks

user1503606
  • 3,872
  • 13
  • 44
  • 78

3 Answers3

4

You need to either run your Node app as a service/daemon using Forever. Or, of you still want to run it on the shell via SSH, use something like screen or tmux.

Using Forever:

How does one start a node.js server as a daemon process?

Using Screen:

http://www.rackaid.com/resources/linux-screen-tutorial-and-how-to/

Using Tmux:

http://blog.hawkhost.com/2010/06/28/tmux-the-terminal-multiplexer/

Community
  • 1
  • 1
JP Richardson
  • 38,609
  • 36
  • 119
  • 151
1

As JP says, Forever is a fine choice.

Meanwhile, if you are running on a Unix variant box of some sort (like LINUX or a Mac) then you already have everything you need. Just run the following:

nohup node server.js &

This is kick off your node app in the background and redirect all console output from the app into a local file named "nohup.out".

The benefit of Forever is it'll restart your server process if it crashes.

Note: neither one alone will restart your server app if the computer restarts.

Leon Stankowski
  • 408
  • 2
  • 3
-1

I believe you'll need to use a third-party tool to keep it running. Something like IISNode if on Windows, or nginx if on linux to manage Node for you.

Khuram Malik
  • 1,475
  • 3
  • 18
  • 30