0

We are building a realtime webapp on sails 0.10.5. We have a lot of models linked through associations. For example we have a User model and a Post model, where a post always has one user.

The populate makes sure that when fetching a user, we also get the posts. We tell the (websocket) client about updates using the subscribe and watch model methods in our sails controllers.

However whenever a websocket client is watching both the User model (watch and subscribed to all users) and the Post model (watch). And a new Post gets created (via another socket for example) our client only receives a new event for Post, while the User changed as well (indirectly through an association).

How can we make sure our clients get those updates as well? Basically we need a way to sync the results of any populated model to our clients.

askmike
  • 1,900
  • 4
  • 21
  • 27

1 Answers1

0

I'm not sure if this is the best way to do that but does the work: Basically, anytime you update a Post you can publish a User "update" event.

// Post.js
module.exports = {

  attributes: {
    title: 'string',
    owner:{
        model:'user'
    }
  },

  afterUpdate: function(post, cb){

    User.publishUpdate(post.owner, {/* props you want to send */});
    cb();

  }
};
singuerinc
  • 447
  • 4
  • 14
  • a lot of overhead: there is no way in sails to see what has changed, if the relation is one-to-many or many-to-many this will publish a lot of updates, most of which are false. Better solution is to use the (not really well documented) addedTo and removedFrom events, note that you can only use those for changed assocs, not changes to assoc data. – askmike Dec 15 '14 at 17:11