How to get current socket object or identifier using sails controller?

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); // return undefined }); }, 

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 }); }); } }, 
+1
source share
1 answer

You need to pass the req object to the method.

 if (req.isSocket) { let socketId = sails.sockets.getId(req); sails.log('socket id: ' + socketId); } 

Since the request is not a socket request, you may need to do something like

  • Send one identifier to the client after logging in.
  • Use the identifier to join the room. (One user per number.)
  • Broadcast messages to the room with the identifier when you need to send a message to the client.

https://gist.github.com/crtr0/2896891

Update:

From Sail Migration Guide

The onConnect lifecycle callback is deprecated. Instead, if you need to do something when a new socket is connected, send a request from a recently connected client to do this. The goal of onConnect has always been to optimize performance (eliminating the need to make this initial extra round with the server), but using it can lead to confusion and race conditions. If you desperately need to eliminate server polling, you can bind the handler directly to sails.io.on ('connect', function (newConnectedSocket) {}) in your bootstrap (config / bootstrap.js). However, please note that this is not recommended. If you do not encounter true performance issues, you should use the strategy mentioned above for your connectivity logic (that is, send the initial request from the client after the socket is connected). Socket requests are lightweight, so it doesn't add any tangible overhead to your application, and it helps make your code more predictable.

 // in some controller if (req.isSocket) { let handshake = req.socket.manager.handshaken[sails.sockets.getId(req)]; if (handshake) { session = handshake.session; } } 
+1
source share

All Articles