Question

I've written up a minimal example of this. The code is posted here: https://gist.github.com/1524725

I start my server, start my client, verify that the connection between the two is successful, and finally kill the server with CTRL+C. When the server dies, the client immediately runs to completion and closes without printing the message in either on_client_close or on_client_disconnect. There is no perceptible delay.

From the reading I've done, because the client process is terminating normally there isn't any chance that the STDOUT buffer isn't being flushed.

It may also be worth noting that when I kill the client instead of the server, the server responds as expected, firing the on_ws_disconnect function and removing the client connection from its list of active clients.

32-bit Ubuntu 11.10 Socket.io v0.8.7 Socket.io-client v0.8.7 NodeJS v0.6.0

Thanks!

--- EDIT ---

Please note that both the client and the server are Node.js processes rather than the conventional web browser client and node.js server.

Was it helpful?

Solution

NEW ANSWER

Definitely a bug in io-client. :(

I was able to fix this by modifying socket.io-client/libs/socket.js. Around line 433, I simply moved the this.publish('disconnect', reason); above if (wasConnected) {.

Socket.prototype.onDisconnect = function (reason) {
    var wasConnected = this.connected;

    this.publish('disconnect', reason);

    this.connected = false;
    this.connecting = false;
    this.open = false;

    if (wasConnected) {
      this.transport.close();
      this.transport.clearTimeouts();

After pressing ctrl+c, the disconnect message fires in roughly ten seconds.

OLD DISCUSSION

To notify client of shutdown events, you would add something like this to demo_server.js:

var logger = io.log;

process.on('uncaughtException', function (err) {
  if( io && io.socket ) {
     io.socket.broadcast.send({type: 'error', msg: err.toString(), stack: err.stack});
  }
  logger.error(err);
  logger.error(err.stack);

  //todo should we have some default resetting (restart server?)
  app.close();
  process.exit(-1);
});

process.on('SIGHUP', function () {
  logger.error('Got SIGHUP signal.');
  if( io && io.socket ) {
     io.socket.broadcast.send({type: 'error', msg: 'server disconnected with SIGHUP'});
  }
  //todo what happens on a sighup??
  //todo if you're using upstart, just call restart node demo_server.js
});

process.on('SIGTERM', function() {
  logger.error('Shutting down.');
  if( io && io.socket ) {
     io.socket.broadcast.send({type: 'error', msg: 'server disconnected with SIGTERM'});
  }
  app.close();
  process.exit(-1);
});

Of course, what you send in the broadcast.send(...) (or even which command you use there) depends on your preference and client structure.

For the client side, you can tell if the server connection is lost using on('disconnect', ...), which you have in your example:

client.on('disconnect', function(data) {
   alert('disconnected from server; reconnecting...');
   // and so on...
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top