node.jssocketssails.jssails.io.js

Sockets update all the users who are subscribed to the model itself expect for the user who got updated


I have user get subscribed to chat users, the user has the ability to make himself online or offline through sockets,

One of the users have the role of the manager, where he can force users to be online or offline, through Http request User.update, and the also I added User.publishUpdate to alert the subscribers.

the problem is when the manager turn user from offline to online status, it appears that user is online to other users expect for the user himself who we updated, and the problem vanish when I enable autoWatch in sails.

here the socket code:

 var query= {authType: 'google'}; //TODO this is temp, currently the staff will see only staff users, until we fix the performance issue
        if (user.dealershipCompany) {
          query = {
            or: [{
              authType: 'google',
              dealershipCompany: null
            }, {
              authType: 'local',
              dealershipCompany: user.dealershipCompany
            }]
          };
        }
        sails.log.info('Chat Users Query:\n', query);
        User.find(query).exec(function (err, chatUsers) {
          if (err) {
            return callback(err, {
              projectedUser: null,
              porjectedChatUsers: null
            });
          } else {
            // remove the logged in users to avoid self messaging
            _.remove(chatUsers, {
              id: userId
            });
          }
          // Subscribe the connected socket to changes in the online state of chatUsers
          User.subscribe(req.socket, chatUsers, 'update');
        });

Here User update:

 update: function (req, res) {
    var id = req.param('id');
    User.findOne(id).exec(function (err, user) {
      if (err)
        res.serverError(err)
      else if (user) {
        var firstName = req.param('firstName') || user.firstName;
        var lastName = req.param('lastName') || user.lastName;
        var role = req.param('role') || user.role;
        var email = req.param('email') || user.email;
        var online = req.param('online') || user.online;
        user.firstName = firstName;
        user.lastName = lastName;
        user.email = email;
        user.role = role;
        user.online = online;
        user.save(function (err, updatedUser) {
          if (err)
            res.serverError(err);

          User.publishUpdate(updatedUser.id, {
            online: updatedUser.online
          }, req);
          res.ok(updatedUser);

        });
      }
      else {
        res.notFound({error: 'User with id: ' + id + ' Could not be found'});
      }

    });
  },

Solution

  • Well, the user doesn't get updated status because sails think that the user himself changed his own status and not the manager, the solution was to create socket listener that listen to the changes of the model and in User.update I send socket with the updatedUserId and so socket listener check if the user.id equal updatedUserId, it change their own online status.