The disconnect event

The last thing we're going to talk about in this section is the disconnect event, which lets you do something on both the server and the client when the connection drops. We'll add an event listener on the client, and do the same thing on the server.

On the client, next to our connect event, we can call socket.on again to listen to a new event. Once again, the name of the event here is the name of a built-in event, so it's only going to work if you type it correctly. This one is called disconnect:

socket.on('disconnect');

The disconnect event is going to fire whenever the connection drops. If the server goes down, the client is going to be able to do something. For now, that something is just going to be log a message, ...

Get Advanced Node.js Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.