title: Display the username of a user who disconnects from the server
body: <p>I'm trying to display a message when a user disconnects from the server. It currently works in that it will display the message to all connected users. However, if I try and use the user variable to display which user has disconnected, it shows the connected clients' name to themselves, rather than the actual user who left. I've tried a couple of things but can't get it to work, and this is what I've come up with.
  In the browser, the following code will display, "undefined has left the chat", when someone disconnects.</p>
  
  <p>How can I correct this to display the actual user's name?</p>
  
  <p>In the script.js:</p>
  
  <pre><code>var user = user;
  if (!user) {
    user = prompt('Please choose a username:');
    if (!user) {
      alert('Your name has been set to "Anonymous"');
    } else {
        alert('Your name has been set to "'+ user +'"');
    } 
  }
  
  socket.emit('user disconnected', {
      user: user
    });
  
  socket.on('user disconnected', (data) =&gt; {
        $('.chat').append(data.user + " has left the chat");
      });
  </code></pre>
  
  <p>In the server.js:</p>
  
  <pre><code>server(socket('disconnect', ctx =&gt; { console.log(ctx.data); ctx.io.emit('user disconnected', ctx.data); }));
  </code></pre>
  
  <p>Additionally, once the user disconnects, 'transport close' is posted to the console, and I'm not sure what that means.</p>