Sails publish(Update) system does not propagate to

2019-09-03 05:32发布

问题:

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.

回答1:

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();

  }
};