Tojó Tojó - 2 months ago 26
Javascript Question

Express - - session. Refer to user id as socket id

I am using I think I was able to setup the basic config by seeing the tutorials:


var clients = [];

var session = require("express-session")({
secret: 'some key',
resave: true,
saveUninitialized: true
var sharedsession = require("");

io.use(function(socket, next){

io.use(sharedsession(session, {

io.on('connection', function(socket) {

console.log("CLIENT CONNECTED");
var session = socket.handshake.session;

socket.on('disconnect', function() {


What I want to be able to do now is to refer to a specific client socket not by the socket but by the session id that should be assigned to that socket. When a user logins this happens:

req.session.user_id = user_id;
//(user_id is equal to DB {0,1,2,3...} ids

I was able to send sockets to specific clients when I did this:

clients[0].emit("to_do",info); // I don't know who is client index 0 compared to the login reference...

I would like to be able to do this or similar:

user_id = 3; // which would have a socket assigned


That would mean each client would have a socket assigned to its previously assigned id. How could I do this so I could specify the socket by that id? I am not experienced at all with all of this so sorry for any big mistakes. Thanks


Your problem can be solved by each socket joining a group named after it's id:

socket.join(;'to_do', info);

//or[0].id).emit('to_do', info);