Socket.io Detect when client disconnects

I'm writing an application where I open up a socket when a user visits my site, record information by sending data over the socket while the user is on the site, and store the information in a database once the user leaves the site.

The problem I am currently facing is that while I can detect when a socket disconnects from my server, I don't know which socket corresponds to what information, so I don't know what information to store into the database.

Below is where I believe I need to put in code

socket.on('disconnect',function() {
  //insert data corresponding to current socket into database
  console.log('The client has disconnected!');
});

Thanks!

It sounds like you want to keep some kind of state associated with the socket connection. I think what you are looking for is socket.io sessions.

Plenty of examples around on the net, this thread has a lot of good information: socket.io and session?

I found this particularly usefull: http://www.danielbaulig.de/socket-ioexpress/