4

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!

Bob Ren
  • 2,139
  • 2
  • 17
  • 16

1 Answers1

2

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/

Community
  • 1
  • 1
Josh Mc
  • 9,911
  • 8
  • 53
  • 66
  • 1
    Thanks, I've been trying to keep my app as simple as possible though. Is there anyway I can use sessions or assign ids to my sockets without using express? – Bob Ren May 09 '12 at 17:57
  • I remember having this exact same issue, I was not able to find anything that worked. I am don't think there are any popular implementations that do not require express/connect. – Josh Mc May 10 '12 at 02:14