Chris Vilches
Chris Vilches

Reputation: 1187

How does a websocket know the server was taken down?

I was playing with websocket a bit (using Sails.js with its built-in socket thing, which is based on Socket.io).

I noticed Chrome receives two frames every 25 seconds. I thought this was some kind of polling to tell the connection was still on.

But then, I cancelled the server and Chrome was notified immediately.

Also I closed the Node process by force with the kill command, and still Chrome was notified, so that means it wasn't Node sending a signal before shutting down the server.

How does this happen?

Upvotes: 3

Views: 1551

Answers (1)

Useless
Useless

Reputation: 67733

Normal TCP socket connections do this, so it'd be surprising if websockets didn't.

The server kernel is responsible for cleaning up when the server process dies/exits/is killed. This includes releasing memory, closing files, and shutting down sockets. Cleanly shutting down a TCP socket requires sending a message to tell the peer.

Interestingly, on some old versions of Windows with userspace winsock, this didn't happen if the server process crashed. On all OS with compliant TCP support, it should be guaranteed unless the kernel itself hangs, the machine loses power, or the network breaks.

Upvotes: 4

Related Questions