Socket.io : Get Client sessionID at any point Socket.io : Get Client sessionID at any point express express

Socket.io : Get Client sessionID at any point


As of version 1.0, you get the client's sessionid this way:

var socket = io();socket.on('connect', function(){    var sessionid = socket.io.engine.id;    ...});


I'm not sure exactly what you mean, because "when the client clicks on something" assumes that you mean 'client-side' (where you can use socket.socket.sessionid) but "store it in an array of clients" assumes you mean 'server-side' (where you can access it as socket.id).

Client-side:

var socket = io.connect();socket.on('connect', function() {  var sessionid = socket.socket.sessionid;  ...});

Server-side:

io.sockets.on('connection', function(socket) {  var sessionid = socket.id;  ...});


I store client id's in server side event handlers simply with:

// server.jsio.sockets.on('connection', function (client) {  client.on('event_name', function (_) {    var clientId = this.id;  }}