Reloading a server in Node.js
Sometimes we could need reload a server without cut off the connections. We can use the "close" call and wait to the established connections, because "close" stops the server from accepting new connections. But in case we are using WebSockets the "close" call is not enough, becase a WebSocket could not finish in days. So we need a way to be able to detect if a WebSocket is in idle status to kill it and the solution is the server-shutdown module (@mitmaro).
This example is based in two different examples:
- Reloading node with no downtime José F. Romaniello. (@jfromaniello)
- WebSockets: Cómo utilizar Socket.io en tus aplicaciones web Carlos Azaustre. (@carlosazaustre)
- In a shell launch the master script:
$ node server/master.js  : Started master with 6974  : Listeting at http://localhost:8100  : A client (wqHAB1KF2Tl0dDrnAAAA) has connected [...]  : Reloading...  : Listeting at http://localhost:8100  : Sending SIGHUP signal to my child 6980  : SIGHUP signal got. Exit graceluffy.  : server end graceluffy.  : A client (pYw7AtZHggIaFVu7AAAA) has connected [...]  : stopping all...  : Sending SIGTERM signal to my child 7011  : SIGTERM signal got. Exit now.
- In another shell we send the signals:
$ kill -SIGHUP 6974 $ kill -SIGTERM 6974