I would like to access the currently connected socket id using the sails.js controller function (v0.12) . sails.sockets.getId (req.socket); shows undefined since this is not a socket request
My goal is to establish the online status of my user in the database upon successful login
login: function (req, res) { Util.login(req, function(){ var socketId = sails.sockets.getId(req.socket); console.log('socketId ===', socketId);
Basically, I would like to access the current user socket object in the controller or to access the current user session object using the on socket method
Also I'm not sure how I can rewrite the old sockets.onConnect Handler
onConnect: function(session, socket) { // Proceed only if the user is logged in if (session.me) { //console.log('test',session); User.findOne({id: session.me}).exec(function(err, user) { var socketId = sails.sockets.getId(socket); user.status = 'online'; user.ip = socket.handshake.address; user.save(function(err) { // Publish this user creation event to every socket watching the User model via User.watch() User.publishCreate(user, socket); }); // Create the session.users hash if it doesn't exist already session.users = session.users || {}; // Save this user in the session, indexed by their socket ID. // This way we can look the user up by socket ID later. session.users[socketId] = user; // Persist the session //session.save(); // Get updates about users being created User.watch(socket); // Send a message to the client with information about the new user sails.sockets.broadcast(socketId, 'user', { verb :'list', data:session.users }); }); } },
sijo vijayan
source share