使用socket io航行js:在onCreate期间如何订阅符合条件的新模型? [英] Sails js with socket io: how to subscribe to new Model that fits criteria, during onCreate?

查看:80
本文介绍了使用socket io航行js:在onCreate期间如何订阅符合条件的新模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Sails js 0.11.

按照文档此处中所述的教程:

我能够创建发布/订阅关系.但是,我只想通知具有字段"company = abc"的用户的更新"事件.因此,在控制器中,请执行以下操作:

subscribe: function(req, res) {
    User.find({company:'abc"}).exec(function(e,listOfUsers){
      User.subscribe(req.socket,listOfUsers,['create','destroy']);
    });
}

这有效.但是,在此套接字处于打开状态并且创建了属于该公司的新用户的情况下,该套接字将不会收到该新用户的通知.

为了解决这个问题,我进入了User模型,并添加了:

beforeCreate: function(user, next) {

 //just need one sample of user from this company.
 //in theory all users of this company are subscribed by same sockets
 User.findOne({company:user.company}).exec(function(e,companyUser){

    // Get all of the sockets that are subscribed to this user
    var subscribers = User.subscribers(companyUser);

    // Subscribe them all to this new user
    _.each(subscribers, function(subscriber) {
      User.subscribe(subscriber.id, user);
    });
     next();
 });

}

但是我得到警告

warn: `Model.subscribe()` called by a non-socket request. Only requests originating from a connected socket may be subscribed. Ignoring...

我不想使用套接字创建用户.有没有解决的办法?只是想让我的套接字订户保持最新状态!

谢谢

解决方案

实际上,将其放入afterCreate并不会真正起作用,因为订户在创建对象时会收到消息,但是您要在创建对象后将其放入因此该事件将不会运行.

但是,只要有技巧,我们就可以完成这项工作.订阅公司而不是用户,这是详细信息:

因此,首先,将您的用户订阅到它的公司中(所有者是我的sails应用中的公司ID):

Company.findOne({id: req.session.user.owner}).exec(function(e, company) {
    Company.subscribe(req.socket, company);
});

然后,在创建用户时,在afterCreate中,在套接字消息中查找公司ID.检索该公司的所有订户,并将其订为用户模型的消息"上下文.然后,在您订阅所有用户后,立即通过Company模型的message()函数向他们发送消息"类型的消息.

  afterCreate: function(message, next) {
    Company.findOne({id: message.owner}).exec(function(e, company) {
        var subscribers = Company.subscribers(company);
        _.each(subscribers, function(subscriber) {
            User.subscribe(subscriber, message, 'message');
        });

        Company.message(company, message.message);
    });

    next();
  },

这里,消息对象是socket.io有效负载.在您向套接字客户端订阅公司的第一部分中,即使您可以创建一个特定的订阅者条件表来存储所有不同的订阅渠道,您也可以使用条件指定要在哪个用户那里进行订阅.

希望有帮助.

参考文献:

http://sailsjs.org/documentation/reference/web-sockets/resourceful-pub-sub/subscribe

http://sailsjs.org/documentation/reference/web-sockets/resourceful-pub-sub/message

I am using Sails js 0.11.

Following the tutorial described on the docs here:

I am able to create a pub/sub relationship. However I only want to be notified of "update" events of users that have the field "company=abc". Therefore in the controller I do the following:

subscribe: function(req, res) {
    User.find({company:'abc"}).exec(function(e,listOfUsers){
      User.subscribe(req.socket,listOfUsers,['create','destroy']);
    });
}

This works. However in the case this socket is open, and a new user belonging to this company is created, the socket won't receive notifications for this new user.

In order to solve that, I went to User model, and added:

beforeCreate: function(user, next) {

 //just need one sample of user from this company.
 //in theory all users of this company are subscribed by same sockets
 User.findOne({company:user.company}).exec(function(e,companyUser){

    // Get all of the sockets that are subscribed to this user
    var subscribers = User.subscribers(companyUser);

    // Subscribe them all to this new user
    _.each(subscribers, function(subscriber) {
      User.subscribe(subscriber.id, user);
    });
     next();
 });

}

However I get the warning

warn: `Model.subscribe()` called by a non-socket request. Only requests originating from a connected socket may be subscribed. Ignoring...

I do not want to create user using sockets. Is there a way around this? Just want to keep my socket subscribers up to date!

Thanks

解决方案

Actually, putting this in the afterCreate won't really work because the subscriber would receive the message when the object created, but you are putting this after its being created so the event won't run.

However with a trick, we can make this work. Subscribe to the Company instead of the User, here are the details:

So first, subscribe your user to it's company (owner is the company id in my sails app):

Company.findOne({id: req.session.user.owner}).exec(function(e, company) {
    Company.subscribe(req.socket, company);
});

Then when you create a User, in the afterCreate, look for the company id in your socket message. Retrieve all of the subscribers of that company, and subscribe them to the "message" context of a User model. Then right after you subscribed all of your user's send a "message" type message to them via the Company model's message() function.

  afterCreate: function(message, next) {
    Company.findOne({id: message.owner}).exec(function(e, company) {
        var subscribers = Company.subscribers(company);
        _.each(subscribers, function(subscriber) {
            User.subscribe(subscriber, message, 'message');
        });

        Company.message(company, message.message);
    });

    next();
  },

Here, the message object is the socket.io payload. In the first section where you subscribe your socket client to the company, you can have criteria to specify which user's to subscribe to where, even you can create a specific subscribers criteria table where you store all the different subscription channels.

Hope it helps.

References:

http://sailsjs.org/documentation/reference/web-sockets/resourceful-pub-sub/subscribe

http://sailsjs.org/documentation/reference/web-sockets/resourceful-pub-sub/message

这篇关于使用socket io航行js:在onCreate期间如何订阅符合条件的新模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆