1

As we all know, when you update a nodejs file and upload it to your server, it needs a restart to be applied -- but if you're using something like nodemon where it automatically restarts your server, your clients that are logged in would need to refresh the screen.

So I was wondering, is there a way I could trigger a "logout" when my server goes down/is restarted so that at the minimum it just refreshes the clients so they know what's happened?

As it stands they'll broadcast undefined errors because theyre session has been lost?

I tried to validate using a function I have called authenticate

function authenticate(req,res,next) {
    if (!req.session.loggedIn) {
        fs.readFile('./html/login.html', "utf8", function(err, html) {
            res.send(html);
        });
    } else next();
}

and here's how I call the function:

app.io.route('move', authenticate, function(req) {

But my server crashes because the parameters are undefined at that point.

Basically think of it like an onbeforeunload event but instead of the browser, the server.

Sterling Archer
  • 22,070
  • 18
  • 81
  • 118

1 Answers1

0

The approach I would take is have something like Socket.IO notify all connected clients that your server is performing a restart, this restart notice, once received, would cause the client to disconnect the socket and poll for the server to come back up. Once it's back up, you can do a hard or soft refresh.

Server                   Server                            
SIGINT/Exit received --> io.broadcast("server:restart") --

  Client                        Client
-> receive "server:restart" --> disconnect socket, poll for server back up --

  Client
-> load refreshed data

You can capture some signal events like shown here and based on some information here you can use the "exit" event.

process.on("exit", function() {
  app.io.sockets.emit("server:shutdown");
});

This will only work if the emit process is syncrhonous (and it looks like it may be). That means you can't get a callback you can only broadcast the message and let the client do the rest.

If you want to "hard refresh" you can poll a "heartbeat" route that just returns if the server is good (and running, obviously) and then do a location.reload() call. For a soft refresh, ping a route that returns new page contents and then page swap ($("body").html(response)).

Community
  • 1
  • 1
Brandon Buck
  • 7,177
  • 2
  • 30
  • 51
  • This does not do anything when `nodemon` restarts the server – Sterling Archer Apr 18 '14 at 22:31
  • You are correct, it appears that nodemon sends a SIGKILL which is not an event you can capture. – Brandon Buck Apr 19 '14 at 00:20
  • Dang, I was scared of that. Thank you though, I appreciate your help. Where did you find that by the way? I'd love to read that – Sterling Archer Apr 19 '14 at 00:27
  • I tested it locally with a small script. The part about Nodemon that is, I tried listening for `"exit"`, `"SIGTERM"` and `"SIGKILL"` (which errored). Of course, from Node.js docs "SIGKILL cannot have a listener installed, it will unconditionally terminate node on all platforms." – Brandon Buck Apr 19 '14 at 05:07