仅为模型的所有者广播消息

我正在构build一个应用程序,用户可以创build事件,其他用户可以“join”并向事件添加注释,也可以打开他们之间的聊天,我有一个名为“通知”的模型,我要存储所有通知系统,并且当用户对他的事件发表评论时,我要提醒事件的主人,给他写一个新的信息等等。

这是我写的评论的代码的一部分:

通知模式:

/* Notification.js */ module.exports = { attributes: { title: { type: 'string', required: true }, text: { type: 'string' }, type:{ type: 'string', enum: ['new_assistant', 'cancel_assistant', 'new_message', 'new_comment'], required: 'true' }, read: { type: 'boolean', defaultsTo: false }, user: { model: 'user' } } }; 

这是我订阅套接字到他的通知模型的地方:

 Notification.find({ user: owner_id }).then(function(notifications) { return Notification.watch(req.socket); }); 

每当用户在一个事件中发表评论,我创build一个新的通知logging:

 Notification.create({ title: 'A new user has comment', text: "Hola", type: 'new_comment', read: false, user: event.owner }).then(function(comment) { return Notification.publishCreate({ id: notification.id, title: 'A new user has comment' }); }); 

该代码运行,但是这发送了一个套接字消息给所有用户,我只是想警告事件的所有者(以及未来的用户谁去这个事件)。

非常感谢。

watch将模型实例创build消息发送到所有正在观察模型的套接字,相同的注册可能已经执行,而不会查找通知,因为它不是实例相关的,即只需调用: Notification.watch(req.socket);

要将通知发送给单个订户,请使用sails套接字

当您想要订阅时为所有者创build一个空间:

 sails.sockets.join(req.socket, owner_id); 

而当你想发布到这个房间的广播:

 Notification.create({ title: 'A new user has comment', text: "Hola", type: 'new_comment', read: false, user: event.owner }).then(function(comment) { sails.sockets.broadcast(event.owner, { id: notification.id, title: 'A new user has comment' }); });