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)
node server/server.js
- In a shell launch the master script:
$ node server/master.js
[6974] : Started master with 6974
[6980] : Listeting at http://localhost:8100
[6980] : A client (wqHAB1KF2Tl0dDrnAAAA) has connected
[...]
[6974] : Reloading...
[7011] : Listeting at http://localhost:8100
[6974] : Sending SIGHUP signal to my child 6980
[6980] : SIGHUP signal got. Exit graceluffy.
[6980] : server end graceluffy.
[7011] : A client (pYw7AtZHggIaFVu7AAAA) has connected
[...]
[6974] : stopping all...
[6974] : Sending SIGTERM signal to my child 7011
[7011] : SIGTERM signal got. Exit now.
- In another shell we send the signals:
$ kill -SIGHUP 6974
$ kill -SIGTERM 6974