Instructor: To deal with a client disconnecting, we'll go into our socket server and open our io.js file. This will be where a client has actually closed their browser and therefore the socket has disconnected.
We're going to listen for an inbuilt event, specifically on the socket, called disconnect.
We're then going to use our IDs map to get hold of the user data, based on the passed in socket ID. If we have some user data, we're just going to log it out to the console for now and we'll actually output the user name.
It could be quite useful to know which user has closed their browser. You could even use the socket server to broadcast all the ever connected clients, the fact that that user has not closed their browser.
On the client side, it might be useful to know when the socket server is connected. To demonstrate that, we're going to modify our pop-up message view component.
Within the sockets block, we can now add the standard disconnect event. When the server disconnects, we're actually going to display our pop-up message. We'll also set a notifications text to warn the user the fact the server is no longer available.
We'll set the color to be red. We'll set the snackbar property to true to display it. Let's now see it in action.
Here, we have four connected clients. They've each got their own individual socket ID. My socket server is running. It shows them as connected.
If I just bring the server out of the way a second and if I actually close the server, we get the error message being displayed using our snackbar component. It seems to work fine.