A snippet help you config socket to get user info from user request socket.

In sails v0.10.x we can get user session information from socket by configure config/sockets.js as the following:

module.exports.sockets = {

  onConnect: function(session, socket) {

    // bind user session to socket
    var user = session.passport && session.passport.user;
    socket.session = session;
    socket.user = user;

    if(user) {
      // get all rooms of user or do what you like
      //Room.find
    }

    sails.log.silly("config/sockets onConnect, user=" + user, session);
  },   onDisconnect: function(session, socket) {

    // By default: do nothing.
  }
}

After that, you can get user info in requests where we can access by calling: socket.user

About The Author