adavia
adavia

Reputation: 423

Cant listen for server events using publishUpdate with sails js

Im trying to publishUpdate some data from the server in order to listen for user/profile creation but im not able to listen to those events in the client using Angular. Is it possible im missing something here? Or maybe something wrong im doing?

// UserController

saveUser: function(req, res) {
 User.create({name: req.param('name'), profile: {aboutMe: req.param('about'), gender: req.param('gender')}})
  .exec(function(err, user) {
    Profile
      .findOneById(user.profile)
      .exec(function(err, profile) {
       profile.user = user.id;
       profile.save(function(error, saved){
         if (error) return res.badRequest('Error.');  
         if (saved) res.json(saved);

         Profile.publishUpdate(saved.id, saved);
       });
     });
   });
}

// Client Angular

$scope.send = createUser;

listenProfile();

function createUser() {        
 var obj = {
   name: $scope.name,
   about: $scope.profile.about,
   gender: $scope.profile.gender

  User.create(obj).then(function(data) {
    console.log(data); // Data displayed correctly
  }, function(error) {
    console.log(error);
  });
}

function listenProfile() {
  io.socket.on('profile',function(data){
    console.log(data);  //Nothing happens?             
  });
};

Upvotes: 1

Views: 299

Answers (2)

Gosh Hernandez
Gosh Hernandez

Reputation: 61

I think that are missing "suscribe" to your model... for example

Profile.subscribe(req.socket, data);

And linked to your app via GET.

io.socket.get("/profile",function(data){
        console.log(data);
    });

Upvotes: 0

tpie
tpie

Reputation: 6221

Try changing it to a capital P:

function listenProfile() {
  io.socket.on('Profile',function(data){
    console.log(data);  //Nothing happens?             
  });
};

Upvotes: 1

Related Questions